Skip to content

Commit ea4e807

Browse files
committed
Add test for OpenSSl modular reduction bug.
1 parent 1e64b70 commit ea4e807

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

Diff for: docs/TESTS.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ of two large primes, sets the generator with order of one prime and tries points
144144

145145
## Edge-Cases
146146
Tests various inputs to ECDH which may cause an implementation to achieve a certain edge-case state during ECDH.
147-
Some of the data is from the google/Wycheproof project. Tests include [CVE-2017-10176](https://nvd.nist.gov/vuln/detail/CVE-2017-10176) and [CVE-2017-8932](https://nvd.nist.gov/vuln/detail/CVE-2017-8932).
147+
Some of the data is from the google/Wycheproof project. Tests include [CVE-2017-10176](https://nvd.nist.gov/vuln/detail/CVE-2017-10176) and [CVE-2017-8932](https://nvd.nist.gov/vuln/detail/CVE-2017-8932) and an OpenSSL modular reduction bug
148+
presented in [Practical realisation and elimination of an ECC-related software bug attack](https://eprint.iacr.org/2011/633).
148149
Various custom edge private key values are also tested.
149150

150151
CVE-2017-10176 was in implementation issue in the SunEC Java library (and NSS ([CVE-2017-7781](https://nvd.nist.gov/vuln/detail/CVE-2017-7781)), thus also anything that used it) that caused the implementation to reach the point at infinity during ECDH computation.

Diff for: src/cz/crcs/ectester/data/other/keys.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<keys xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="../schema.xsd">
4+
<privkey>
5+
<id>openssl-bug/skey</id>
6+
<inline>0x4543A4D2C9DDD5516FD16D5498034C76D4EAB340276B6BAD8FF57756F4ECA8E6</inline>
7+
<curve>secg/secp256r1</curve>
8+
</privkey>
9+
<pubkey>
10+
<id>openssl-bug/pkey</id>
11+
<inline>0x296D416994A4801B9A48E8C67C98E0C05DE1C0E85D4DC676F32FEACDC4998F0E,0xA91F9BE06C1D50EEB0295A35CA0F130F17EA647147626318E28AEC97F0653749</inline>
12+
<curve>secg/secp256r1</curve>
13+
</pubkey>
14+
</keys>

Diff for: src/cz/crcs/ectester/data/other/results.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="../schema.xsd">
4+
<kaResult>
5+
<id>openssl-bug</id>
6+
<ka>DH</ka>
7+
<inline>0xdb6f7cd6a06846bf9da9b4928caa5e4b7c8f58d9</inline>
8+
<!-- == SHA1(0x1D0F27241C177385B0D5025029FABD5D5D8475DA4E267DCD177B49C63605C25A) -->
9+
<curve>secg/secp256r1</curve>
10+
<onekey>other/openssl-bug/pkey</onekey>
11+
<otherkey>other/openssl-bug/skey</otherkey>
12+
<desc>https://eprint.iacr.org/2011/633</desc>
13+
</kaResult>
14+
</results>

Diff for: src/cz/crcs/ectester/reader/test/CardEdgeCasesSuite.java

+27
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ public Result apply(CommandTestable testable) {
110110
doTest(CompoundTest.all(Result.ExpectedValue.SUCCESS, description, groupTests.toArray(new Test[0])));
111111
}
112112

113+
{
114+
EC_KAResult openssl_bug = EC_Store.getInstance().getObject(EC_KAResult.class, "other", "openssl-bug");
115+
EC_Curve curve = EC_Store.getInstance().getObject(EC_Curve.class, openssl_bug.getCurve());
116+
EC_Key.Private skey = EC_Store.getInstance().getObject(EC_Key.Private.class, openssl_bug.getOtherKey());
117+
EC_Key.Public pkey = EC_Store.getInstance().getObject(EC_Key.Public.class, openssl_bug.getOneKey());
118+
Test key = CommandTest.expect(new Command.Allocate(this.card, ECTesterApplet.KEYPAIR_BOTH, curve.getBits(), KeyPair.ALG_EC_FP), Result.ExpectedValue.SUCCESS);
119+
Test set = CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_BOTH, EC_Consts.CURVE_external, curve.getParams(), curve.flatten()), Result.ExpectedValue.SUCCESS);
120+
Test setPrivate = CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_LOCAL, EC_Consts.CURVE_external, EC_Consts.PARAMETER_S, skey.flatten(EC_Consts.PARAMETER_S)), Result.ExpectedValue.SUCCESS);
121+
Test setPublic = CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_REMOTE, EC_Consts.CURVE_external, EC_Consts.PARAMETER_W, pkey.flatten(EC_Consts.PARAMETER_W)), Result.ExpectedValue.SUCCESS);
122+
Test ecdh = CommandTest.function(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.EXPORT_TRUE, EC_Consts.TRANSFORMATION_NONE, openssl_bug.getJavaCardKA()), new TestCallback<CommandTestable>() {
123+
@Override
124+
public Result apply(CommandTestable testable) {
125+
Response.ECDH dh = (Response.ECDH) testable.getResponse();
126+
if (!dh.successful())
127+
return new Result(Result.Value.FAILURE, "ECDH was unsuccessful.");
128+
if (!dh.hasSecret())
129+
return new Result(Result.Value.FAILURE, "ECDH response did not contain the derived secret.");
130+
if (ByteUtil.compareBytes(dh.getSecret(), 0, openssl_bug.getData(0), 0, dh.secretLength())) {
131+
return new Result(Result.Value.FAILURE, "OpenSSL bug is present, derived secret matches example.");
132+
}
133+
return new Result(Result.Value.SUCCESS);
134+
}
135+
});
136+
137+
doTest(CompoundTest.greedyAll(Result.ExpectedValue.SUCCESS, "Test OpenSSL modular reduction bug.", key, set, setPrivate, setPublic, ecdh));
138+
}
139+
113140
Map<String, EC_Curve> curveMap = EC_Store.getInstance().getObjects(EC_Curve.class, "secg");
114141
List<EC_Curve> curves = curveMap.entrySet().stream().filter((e) -> e.getKey().endsWith("r1") && e.getValue().getField() == KeyPair.ALG_EC_FP).map(Map.Entry::getValue).collect(Collectors.toList());
115142
curves.add(EC_Store.getInstance().getObject(EC_Curve.class, "cofactor/cofactor128p2"));

0 commit comments

Comments
 (0)