Skip to content

Commit 710e080

Browse files
committed
Crypto: Refactor casing and documentation
1 parent 8ee03e4 commit 710e080

File tree

4 files changed

+139
-136
lines changed

4 files changed

+139
-136
lines changed

cpp/ql/lib/experimental/quantum/OpenSSL/AlgorithmInstances/MACAlgorithmInstance.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ private import experimental.quantum.OpenSSL.Operations.OpenSSLOperations
77
private import AlgToAVCFlow
88

99
class KnownOpenSslMacConstantAlgorithmInstance extends OpenSslAlgorithmInstance,
10-
Crypto::MACAlgorithmInstance instanceof KnownOpenSslMacAlgorithmExpr
10+
Crypto::MacAlgorithmInstance instanceof KnownOpenSslMacAlgorithmExpr
1111
{
1212
OpenSslAlgorithmValueConsumer getterCall;
1313

@@ -39,14 +39,14 @@ class KnownOpenSslMacConstantAlgorithmInstance extends OpenSslAlgorithmInstance,
3939
result = this.(Call).getTarget().getName()
4040
}
4141

42-
override Crypto::TMACType getMacType() {
42+
override Crypto::TMacType getMacType() {
4343
this instanceof KnownOpenSslHMacAlgorithmExpr and result instanceof Crypto::THMAC
4444
or
4545
this instanceof KnownOpenSslCMacAlgorithmExpr and result instanceof Crypto::TCMAC
4646
}
4747
}
4848

49-
class KnownOpenSslHMacConstantAlgorithmInstance extends Crypto::HMACAlgorithmInstance,
49+
class KnownOpenSslHMacConstantAlgorithmInstance extends Crypto::HmacAlgorithmInstance,
5050
KnownOpenSslMacConstantAlgorithmInstance
5151
{
5252
override Crypto::AlgorithmValueConsumer getHashAlgorithmValueConsumer() {

cpp/ql/lib/experimental/quantum/OpenSSL/AlgorithmInstances/PaddingAlgorithmInstance.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,18 @@ class KnownOpenSslPaddingConstantAlgorithmInstance extends OpenSslAlgorithmInsta
161161
// else result = Crypto::OtherPadding()
162162
// }
163163
// }
164-
class OaepPaddingAlgorithmInstance extends Crypto::OAEPPaddingAlgorithmInstance,
164+
class OaepPaddingAlgorithmInstance extends Crypto::OaepPaddingAlgorithmInstance,
165165
KnownOpenSslPaddingConstantAlgorithmInstance
166166
{
167167
OaepPaddingAlgorithmInstance() {
168168
this.(Crypto::PaddingAlgorithmInstance).getPaddingType() = Crypto::OAEP()
169169
}
170170

171-
override Crypto::HashAlgorithmInstance getOAEPEncodingHashAlgorithm() {
171+
override Crypto::HashAlgorithmInstance getOaepEncodingHashAlgorithm() {
172172
none() //TODO
173173
}
174174

175-
override Crypto::HashAlgorithmInstance getMGF1HashAlgorithm() {
175+
override Crypto::HashAlgorithmInstance getMgf1HashAlgorithm() {
176176
none() //TODO
177177
}
178178
}

java/ql/lib/experimental/quantum/JCA.qll

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ module JCAModel {
205205
}
206206

207207
bindingset[name]
208-
predicate mac_name_to_mac_type_known(Crypto::TMACType type, string name) {
208+
predicate mac_name_to_mac_type_known(Crypto::TMacType type, string name) {
209209
type = Crypto::THMAC() and
210210
name.toUpperCase().matches("HMAC%")
211211
}
@@ -373,12 +373,12 @@ module JCAModel {
373373
oaep_padding_string_components(any(CipherStringLiteral s).getPadding(), hash, mfg)
374374
}
375375

376-
class OAEPPaddingHashAlgorithmInstance extends OAEPPaddingAlgorithmInstance,
376+
class OaepPaddingHashAlgorithmInstance extends OaepPaddingAlgorithmInstance,
377377
Crypto::HashAlgorithmInstance instanceof CipherStringLiteral
378378
{
379379
string hashName;
380380

381-
OAEPPaddingHashAlgorithmInstance() {
381+
OaepPaddingHashAlgorithmInstance() {
382382
oaep_padding_string_components(super.getPadding(), hashName, _)
383383
}
384384

@@ -389,12 +389,12 @@ module JCAModel {
389389
override int getFixedDigestLength() { exists(hash_name_to_type_known(hashName, result)) }
390390
}
391391

392-
class OAEPPaddingAlgorithmInstance extends Crypto::OAEPPaddingAlgorithmInstance,
392+
class OaepPaddingAlgorithmInstance extends Crypto::OaepPaddingAlgorithmInstance,
393393
CipherStringLiteralPaddingAlgorithmInstance
394394
{
395-
override Crypto::HashAlgorithmInstance getOAEPEncodingHashAlgorithm() { result = this }
395+
override Crypto::HashAlgorithmInstance getOaepEncodingHashAlgorithm() { result = this }
396396

397-
override Crypto::HashAlgorithmInstance getMGF1HashAlgorithm() { none() } // TODO
397+
override Crypto::HashAlgorithmInstance getMgf1HashAlgorithm() { none() } // TODO
398398
}
399399

400400
/**
@@ -1156,9 +1156,7 @@ module JCAModel {
11561156
}
11571157

11581158
module KeySpecInstantiationToGenerateSecretFlowConfig implements DataFlow::ConfigSig {
1159-
predicate isSource(DataFlow::Node src) {
1160-
exists(KeySpecInstantiation call | src.asExpr() = call)
1161-
}
1159+
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof KeySpecInstantiation }
11621160

11631161
predicate isSink(DataFlow::Node sink) {
11641162
exists(SecretKeyFactoryGenerateSecretCall call | sink.asExpr() = call.getKeySpecArg())
@@ -1207,29 +1205,29 @@ module JCAModel {
12071205
predicate isIntermediate() { none() }
12081206
}
12091207

1210-
class KDFAlgorithmStringLiteral extends Crypto::KeyDerivationAlgorithmInstance instanceof StringLiteral
1208+
class KdfAlgorithmStringLiteral extends Crypto::KeyDerivationAlgorithmInstance instanceof StringLiteral
12111209
{
12121210
SecretKeyFactoryKDFAlgorithmValueConsumer consumer;
12131211

1214-
KDFAlgorithmStringLiteral() {
1212+
KdfAlgorithmStringLiteral() {
12151213
kdf_names(this.getValue()) and
12161214
KDFAlgorithmStringToGetInstanceFlow::flow(DataFlow::exprNode(this), consumer.getInputNode())
12171215
}
12181216

1219-
override string getRawKDFAlgorithmName() { result = super.getValue() }
1217+
override string getRawKdfAlgorithmName() { result = super.getValue() }
12201218

1221-
override Crypto::TKeyDerivationType getKDFType() {
1219+
override Crypto::TKeyDerivationType getKdfType() {
12221220
result = kdf_name_to_kdf_type(super.getValue(), _)
12231221
}
12241222

12251223
SecretKeyFactoryKDFAlgorithmValueConsumer getConsumer() { result = consumer }
12261224
}
12271225

1228-
class PBKDF2AlgorithmStringLiteral extends KDFAlgorithmStringLiteral,
1229-
Crypto::PBKDF2AlgorithmInstance, Crypto::HMACAlgorithmInstance, Crypto::HashAlgorithmInstance,
1226+
class Pbkdf2AlgorithmStringLiteral extends KdfAlgorithmStringLiteral,
1227+
Crypto::Pbkdf2AlgorithmInstance, Crypto::HmacAlgorithmInstance, Crypto::HashAlgorithmInstance,
12301228
Crypto::AlgorithmValueConsumer
12311229
{
1232-
PBKDF2AlgorithmStringLiteral() { super.getKDFType() instanceof Crypto::PBKDF2 }
1230+
Pbkdf2AlgorithmStringLiteral() { super.getKdfType() instanceof Crypto::PBKDF2 }
12331231

12341232
override Crypto::ConsumerInputDataFlowNode getInputNode() { none() }
12351233

@@ -1244,16 +1242,16 @@ module JCAModel {
12441242
}
12451243

12461244
override string getRawMacAlgorithmName() {
1247-
result = super.getRawKDFAlgorithmName().splitAt("PBKDF2With", 1)
1245+
result = super.getRawKdfAlgorithmName().splitAt("PBKDF2With", 1)
12481246
}
12491247

12501248
override string getRawHashAlgorithmName() {
1251-
result = super.getRawKDFAlgorithmName().splitAt("WithHmac", 1)
1249+
result = super.getRawKdfAlgorithmName().splitAt("WithHmac", 1)
12521250
}
12531251

1254-
override Crypto::TMACType getMacType() { result instanceof Crypto::THMAC }
1252+
override Crypto::TMacType getMacType() { result instanceof Crypto::THMAC }
12551253

1256-
override Crypto::AlgorithmValueConsumer getHMACAlgorithmValueConsumer() { result = this }
1254+
override Crypto::AlgorithmValueConsumer getHmacAlgorithmValueConsumer() { result = this }
12571255

12581256
override Crypto::AlgorithmValueConsumer getHashAlgorithmValueConsumer() { result = this }
12591257
}
@@ -1267,7 +1265,7 @@ module JCAModel {
12671265
override Crypto::ConsumerInputDataFlowNode getInputNode() { result.asExpr() = this }
12681266

12691267
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() {
1270-
exists(KDFAlgorithmStringLiteral l | l.getConsumer() = this and result = l)
1268+
exists(KdfAlgorithmStringLiteral l | l.getConsumer() = this and result = l)
12711269
}
12721270

12731271
SecretKeyFactoryGetInstanceCall getInstantiation() { result = call }
@@ -1442,105 +1440,105 @@ module JCAModel {
14421440
* MACs
14431441
*/
14441442

1445-
module MACKnownAlgorithmToConsumerConfig implements DataFlow::ConfigSig {
1443+
module MacKnownAlgorithmToConsumerConfig implements DataFlow::ConfigSig {
14461444
predicate isSource(DataFlow::Node src) { mac_names(src.asExpr().(StringLiteral).getValue()) }
14471445

14481446
predicate isSink(DataFlow::Node sink) {
1449-
exists(MACGetInstanceCall call | sink.asExpr() = call.getAlgorithmArg())
1447+
exists(MacGetInstanceCall call | sink.asExpr() = call.getAlgorithmArg())
14501448
}
14511449
}
14521450

1453-
module MACKnownAlgorithmToConsumerFlow = DataFlow::Global<MACKnownAlgorithmToConsumerConfig>;
1451+
module MacKnownAlgorithmToConsumerFlow = DataFlow::Global<MacKnownAlgorithmToConsumerConfig>;
14541452

1455-
module MACGetInstanceToMACOperationFlowConfig implements DataFlow::ConfigSig {
1456-
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof MACGetInstanceCall }
1453+
module MacGetInstanceToMacOperationFlowConfig implements DataFlow::ConfigSig {
1454+
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof MacGetInstanceCall }
14571455

14581456
predicate isSink(DataFlow::Node sink) {
1459-
exists(MACOperationCall call | sink.asExpr() = call.(MethodCall).getQualifier()) or
1460-
exists(MACInitCall call | sink.asExpr() = call.(MethodCall).getQualifier())
1457+
exists(MacOperationCall call | sink.asExpr() = call.(MethodCall).getQualifier()) or
1458+
exists(MacInitCall call | sink.asExpr() = call.(MethodCall).getQualifier())
14611459
}
14621460
}
14631461

1464-
module MACGetInstanceToMACOperationFlow =
1465-
DataFlow::Global<MACGetInstanceToMACOperationFlowConfig>;
1462+
module MacGetInstanceToMacOperationFlow =
1463+
DataFlow::Global<MacGetInstanceToMacOperationFlowConfig>;
14661464

1467-
module MACInitCallToMACOperationFlowConfig implements DataFlow::ConfigSig {
1465+
module MacInitCallToMacOperationFlowConfig implements DataFlow::ConfigSig {
14681466
// TODO: use flow state with one config
14691467
predicate isSource(DataFlow::Node src) {
1470-
exists(MACInitCall init | src.asExpr() = init.getQualifier())
1468+
exists(MacInitCall init | src.asExpr() = init.getQualifier())
14711469
}
14721470

14731471
predicate isSink(DataFlow::Node sink) {
1474-
exists(MACOperationCall call | sink.asExpr() = call.(MethodCall).getQualifier())
1472+
exists(MacOperationCall call | sink.asExpr() = call.(MethodCall).getQualifier())
14751473
}
14761474
}
14771475

1478-
module MACInitCallToMACOperationFlow = DataFlow::Global<MACInitCallToMACOperationFlowConfig>;
1476+
module MacInitCallToMacOperationFlow = DataFlow::Global<MacInitCallToMacOperationFlowConfig>;
14791477

1480-
class KnownMACAlgorithm extends Crypto::MACAlgorithmInstance instanceof StringLiteral {
1481-
MACGetInstanceAlgorithmValueConsumer consumer;
1478+
class KnownMacAlgorithm extends Crypto::MacAlgorithmInstance instanceof StringLiteral {
1479+
MacGetInstanceAlgorithmValueConsumer consumer;
14821480

1483-
KnownMACAlgorithm() {
1481+
KnownMacAlgorithm() {
14841482
mac_names(this.getValue()) and
1485-
MACKnownAlgorithmToConsumerFlow::flow(DataFlow::exprNode(this), consumer.getInputNode())
1483+
MacKnownAlgorithmToConsumerFlow::flow(DataFlow::exprNode(this), consumer.getInputNode())
14861484
}
14871485

1488-
MACGetInstanceAlgorithmValueConsumer getConsumer() { result = consumer }
1486+
MacGetInstanceAlgorithmValueConsumer getConsumer() { result = consumer }
14891487

14901488
override string getRawMacAlgorithmName() { result = super.getValue() }
14911489

1492-
override Crypto::TMACType getMacType() {
1490+
override Crypto::TMacType getMacType() {
14931491
if mac_name_to_mac_type_known(_, super.getValue())
14941492
then mac_name_to_mac_type_known(result, super.getValue())
14951493
else result instanceof Crypto::TOtherMACType
14961494
}
14971495
}
14981496

1499-
class MACGetInstanceCall extends MethodCall {
1500-
MACGetInstanceCall() { this.getCallee().hasQualifiedName("javax.crypto", "Mac", "getInstance") }
1497+
class MacGetInstanceCall extends MethodCall {
1498+
MacGetInstanceCall() { this.getCallee().hasQualifiedName("javax.crypto", "Mac", "getInstance") }
15011499

15021500
Expr getAlgorithmArg() { result = this.getArgument(0) }
15031501

1504-
MACOperationCall getOperation() {
1505-
MACGetInstanceToMACOperationFlow::flow(DataFlow::exprNode(this),
1502+
MacOperationCall getOperation() {
1503+
MacGetInstanceToMacOperationFlow::flow(DataFlow::exprNode(this),
15061504
DataFlow::exprNode(result.(MethodCall).getQualifier()))
15071505
}
15081506

1509-
MACInitCall getInitCall() {
1510-
MACGetInstanceToMACOperationFlow::flow(DataFlow::exprNode(this),
1507+
MacInitCall getInitCall() {
1508+
MacGetInstanceToMacOperationFlow::flow(DataFlow::exprNode(this),
15111509
DataFlow::exprNode(result.getQualifier()))
15121510
}
15131511
}
15141512

1515-
class MACInitCall extends MethodCall {
1516-
MACInitCall() { this.getCallee().hasQualifiedName("javax.crypto", "Mac", "init") }
1513+
class MacInitCall extends MethodCall {
1514+
MacInitCall() { this.getCallee().hasQualifiedName("javax.crypto", "Mac", "init") }
15171515

15181516
Expr getKeyArg() {
15191517
result = this.getArgument(0) and this.getMethod().getParameterType(0).hasName("Key")
15201518
}
15211519

1522-
MACOperationCall getOperation() {
1523-
MACInitCallToMACOperationFlow::flow(DataFlow::exprNode(this.getQualifier()),
1520+
MacOperationCall getOperation() {
1521+
MacInitCallToMacOperationFlow::flow(DataFlow::exprNode(this.getQualifier()),
15241522
DataFlow::exprNode(result.(MethodCall).getQualifier()))
15251523
}
15261524
}
15271525

1528-
class MACGetInstanceAlgorithmValueConsumer extends Crypto::AlgorithmValueConsumer {
1529-
MACGetInstanceCall call;
1526+
class MacGetInstanceAlgorithmValueConsumer extends Crypto::AlgorithmValueConsumer {
1527+
MacGetInstanceCall call;
15301528

1531-
MACGetInstanceAlgorithmValueConsumer() { this = call.getAlgorithmArg() }
1529+
MacGetInstanceAlgorithmValueConsumer() { this = call.getAlgorithmArg() }
15321530

15331531
override Crypto::ConsumerInputDataFlowNode getInputNode() { result.asExpr() = this }
15341532

15351533
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() {
1536-
exists(KnownMACAlgorithm l | l.getConsumer() = this and result = l)
1534+
exists(KnownMacAlgorithm l | l.getConsumer() = this and result = l)
15371535
}
15381536
}
15391537

1540-
class MACOperationCall extends Crypto::MACOperationInstance instanceof MethodCall {
1538+
class MacOperationCall extends Crypto::MacOperationInstance instanceof MethodCall {
15411539
Expr output;
15421540

1543-
MACOperationCall() {
1541+
MacOperationCall() {
15441542
super.getMethod().getDeclaringType().hasQualifiedName("javax.crypto", "Mac") and
15451543
(
15461544
super.getMethod().hasStringSignature(["doFinal()", "doFinal(byte[])"]) and this = output
@@ -1551,13 +1549,13 @@ module JCAModel {
15511549
}
15521550

15531551
override Crypto::AlgorithmValueConsumer getAnAlgorithmValueConsumer() {
1554-
exists(MACGetInstanceCall instantiation |
1552+
exists(MacGetInstanceCall instantiation |
15551553
instantiation.getOperation() = this and result = instantiation.getAlgorithmArg()
15561554
)
15571555
}
15581556

15591557
override Crypto::ConsumerInputDataFlowNode getKeyConsumer() {
1560-
exists(MACGetInstanceCall instantiation, MACInitCall initCall |
1558+
exists(MacGetInstanceCall instantiation, MacInitCall initCall |
15611559
instantiation.getOperation() = this and
15621560
initCall.getOperation() = this and
15631561
instantiation.getInitCall() = initCall and

0 commit comments

Comments
 (0)