mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Merge pull request #12168 from RasmusWL/crypto-stdlib-modeling
Python: Add modeling of `hmac`
This commit is contained in:
4
python/ql/lib/change-notes/2023-02-13-hmac-modeling.md
Normal file
4
python/ql/lib/change-notes/2023-02-13-hmac-modeling.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
---
|
||||||
|
category: minorAnalysis
|
||||||
|
---
|
||||||
|
* Added modeling of cryptographic operations in the `hmac` library.
|
||||||
@@ -2669,6 +2669,7 @@ private module StdlibPrivate {
|
|||||||
|
|
||||||
HashlibNewCall() {
|
HashlibNewCall() {
|
||||||
this = hashlibNewCall(hashName) and
|
this = hashlibNewCall(hashName) and
|
||||||
|
// we only want to consider it as an cryptographic operation if the input is available
|
||||||
exists(this.getParameter(1, "data"))
|
exists(this.getParameter(1, "data"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2751,6 +2752,78 @@ private module StdlibPrivate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// hmac
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
abstract class HmacCryptographicOperation extends Cryptography::CryptographicOperation::Range,
|
||||||
|
API::CallNode {
|
||||||
|
abstract API::Node getDigestArg();
|
||||||
|
|
||||||
|
override Cryptography::CryptographicAlgorithm getAlgorithm() {
|
||||||
|
exists(string algorithmName | result.matchesName(algorithmName) |
|
||||||
|
this.getDigestArg().asSink() = hashlibMember(algorithmName).asSource()
|
||||||
|
or
|
||||||
|
this.getDigestArg().getAValueReachingSink().asExpr().(StrConst).getText() = algorithmName
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override Cryptography::BlockMode getBlockMode() { none() }
|
||||||
|
}
|
||||||
|
|
||||||
|
API::CallNode getHmacConstructorCall(API::Node digestArg) {
|
||||||
|
result = API::moduleImport("hmac").getMember(["new", "HMAC"]).getACall() and
|
||||||
|
digestArg = result.getParameter(2, "digestmod")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A call to `hmac.new`/`hmac.HMAC`.
|
||||||
|
*
|
||||||
|
* See https://docs.python.org/3.11/library/hmac.html#hmac.new
|
||||||
|
*/
|
||||||
|
class HmacNewCall extends HmacCryptographicOperation {
|
||||||
|
API::Node digestArg;
|
||||||
|
|
||||||
|
HmacNewCall() {
|
||||||
|
this = getHmacConstructorCall(digestArg) and
|
||||||
|
// we only want to consider it as an cryptographic operation if the input is available
|
||||||
|
exists(this.getParameter(1, "msg").asSink())
|
||||||
|
}
|
||||||
|
|
||||||
|
override API::Node getDigestArg() { result = digestArg }
|
||||||
|
|
||||||
|
override DataFlow::Node getAnInput() { result = this.getParameter(1, "msg").asSink() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A call to `.update` on an HMAC object.
|
||||||
|
*
|
||||||
|
* See https://docs.python.org/3.11/library/hmac.html#hmac.HMAC.update
|
||||||
|
*/
|
||||||
|
class HmacUpdateCall extends HmacCryptographicOperation {
|
||||||
|
API::Node digestArg;
|
||||||
|
|
||||||
|
HmacUpdateCall() {
|
||||||
|
this = getHmacConstructorCall(digestArg).getReturn().getMember("update").getACall()
|
||||||
|
}
|
||||||
|
|
||||||
|
override API::Node getDigestArg() { result = digestArg }
|
||||||
|
|
||||||
|
override DataFlow::Node getAnInput() { result = this.getParameter(0, "msg").asSink() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A call to `hmac.digest`.
|
||||||
|
*
|
||||||
|
* See https://docs.python.org/3.11/library/hmac.html#hmac.digest
|
||||||
|
*/
|
||||||
|
class HmacDigestCall extends HmacCryptographicOperation {
|
||||||
|
HmacDigestCall() { this = API::moduleImport("hmac").getMember("digest").getACall() }
|
||||||
|
|
||||||
|
override API::Node getDigestArg() { result = this.getParameter(2, "digest") }
|
||||||
|
|
||||||
|
override DataFlow::Node getAnInput() { result = this.getParameter(1, "msg").asSink() }
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// logging
|
// logging
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
33
python/ql/test/library-tests/frameworks/stdlib/test_hmac.py
Normal file
33
python/ql/test/library-tests/frameworks/stdlib/test_hmac.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import hmac
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
key = b"<secret key>"
|
||||||
|
|
||||||
|
hmac_obj = hmac.new(key, b"secret message", "sha256") # $ CryptographicOperation CryptographicOperationInput=b"secret message" CryptographicOperationAlgorithm=SHA256
|
||||||
|
print(hmac_obj.digest())
|
||||||
|
print(hmac_obj.hexdigest())
|
||||||
|
|
||||||
|
hmac_obj = hmac.new(key, msg=b"secret message", digestmod="sha256") # $ CryptographicOperation CryptographicOperationInput=b"secret message" CryptographicOperationAlgorithm=SHA256
|
||||||
|
print(hmac_obj.hexdigest())
|
||||||
|
|
||||||
|
|
||||||
|
hmac_obj = hmac.new(key, digestmod="sha256")
|
||||||
|
hmac_obj.update(b"secret") # $ CryptographicOperation CryptographicOperationInput=b"secret" CryptographicOperationAlgorithm=SHA256
|
||||||
|
hmac_obj.update(msg=b" message") # $ CryptographicOperation CryptographicOperationInput=b" message" CryptographicOperationAlgorithm=SHA256
|
||||||
|
print(hmac_obj.hexdigest())
|
||||||
|
|
||||||
|
|
||||||
|
hmac_obj = hmac.new(key, b"secret message", hashlib.sha256) # $ CryptographicOperation CryptographicOperationInput=b"secret message" CryptographicOperationAlgorithm=SHA256
|
||||||
|
print(hmac_obj.hexdigest())
|
||||||
|
|
||||||
|
|
||||||
|
# like hmac.new
|
||||||
|
hmac_obj = hmac.HMAC(key, digestmod="sha256")
|
||||||
|
hmac_obj.update(b"secret message") # $ CryptographicOperation CryptographicOperationInput=b"secret message" CryptographicOperationAlgorithm=SHA256
|
||||||
|
print(hmac_obj.hexdigest())
|
||||||
|
|
||||||
|
|
||||||
|
dig = hmac.digest(key, b"secret message", "sha256") # $ CryptographicOperation CryptographicOperationInput=b"secret message" CryptographicOperationAlgorithm=SHA256
|
||||||
|
print(dig)
|
||||||
|
dig = hmac.digest(key, msg=b"secret message", digest="sha256") # $ CryptographicOperation CryptographicOperationInput=b"secret message" CryptographicOperationAlgorithm=SHA256
|
||||||
|
print(dig)
|
||||||
Reference in New Issue
Block a user