Skip to content

Commit efccc6d

Browse files
committed
Refactoring in DSTU4145 code
1 parent e4ff2c1 commit efccc6d

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

core/src/main/java/org/bouncycastle/asn1/ua/DSTU4145NamedCurves.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ public class DSTU4145NamedCurves
1212
{
1313
private static final BigInteger ZERO = BigInteger.valueOf(0);
1414
private static final BigInteger ONE = BigInteger.valueOf(1);
15+
private static final BigInteger TWO = BigInteger.valueOf(2);
16+
private static final BigInteger FOUR = BigInteger.valueOf(4);
1517

16-
static final ECDomainParameters[] params = new ECDomainParameters[10];
17-
static final ASN1ObjectIdentifier[] oids = new ASN1ObjectIdentifier[10];
18+
private static final ECDomainParameters[] DOMAIN_PARAMETERS = new ECDomainParameters[10];
19+
private static final ASN1ObjectIdentifier[] OIDS = new ASN1ObjectIdentifier[10];
1820

1921
//All named curves have the following oid format: 1.2.804.2.1.1.1.1.3.1.1.2.X
2022
//where X is the curve number 0-9
21-
static final String oidBase = UAObjectIdentifiers.dstu4145le.getId() + ".2.";
23+
private static final ASN1ObjectIdentifier OID_BASE = UAObjectIdentifiers.dstu4145le.branch("2");
2224

2325
private static ECPoint configureBasepoint(ECCurve curve, BigInteger x, BigInteger y)
2426
{
@@ -42,16 +44,16 @@ private static ECPoint configureBasepoint(ECCurve curve, BigInteger x, BigIntege
4244
n_s[9] = new BigInteger("3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBA3175458009A8C0A724F02F81AA8A1FCBAF80D90C7A95110504CF", 16);
4345

4446
BigInteger[] h_s = new BigInteger[10];
45-
h_s[0] = BigInteger.valueOf(2);
46-
h_s[1] = BigInteger.valueOf(2);
47-
h_s[2] = BigInteger.valueOf(4);
48-
h_s[3] = BigInteger.valueOf(2);
49-
h_s[4] = BigInteger.valueOf(2);
50-
h_s[5] = BigInteger.valueOf(2);
51-
h_s[6] = BigInteger.valueOf(4);
52-
h_s[7] = BigInteger.valueOf(2);
53-
h_s[8] = BigInteger.valueOf(2);
54-
h_s[9] = BigInteger.valueOf(2);
47+
h_s[0] = TWO;
48+
h_s[1] = TWO;
49+
h_s[2] = FOUR;
50+
h_s[3] = TWO;
51+
h_s[4] = TWO;
52+
h_s[5] = TWO;
53+
h_s[6] = FOUR;
54+
h_s[7] = TWO;
55+
h_s[8] = TWO;
56+
h_s[9] = TWO;
5557

5658
ECCurve.F2m[] curves = new ECCurve.F2m[10];
5759
curves[0] = new ECCurve.F2m(163, 3, 6, 7, ONE, new BigInteger("5FF6108462A2DC8210AB403925E638A19C1455D21", 16), n_s[0], h_s[0]);
@@ -77,14 +79,10 @@ private static ECPoint configureBasepoint(ECCurve curve, BigInteger x, BigIntege
7779
points[8] = configureBasepoint(curves[8], new BigInteger("324A6EDDD512F08C49A99AE0D3F961197A76413E7BE81A400CA681E09639B5FE12E59A109F78BF4A373541B3B9A1", 16), new BigInteger("1AB597A5B4477F59E39539007C7F977D1A567B92B043A49C6B61984C3FE3481AAF454CD41BA1F051626442B3C10", 16));
7880
points[9] = configureBasepoint(curves[9], new BigInteger("1A62BA79D98133A16BBAE7ED9A8E03C32E0824D57AEF72F88986874E5AAE49C27BED49A2A95058068426C2171E99FD3B43C5947C857D", 16), new BigInteger("70B5E1E14031C1F70BBEFE96BDDE66F451754B4CA5F48DA241F331AA396B8D1839A855C1769B1EA14BA53308B5E2723724E090E02DB9", 16));
7981

80-
for (int i = 0; i < params.length; i++)
82+
for (int i = 0; i < 10; i++)
8183
{
82-
params[i] = new ECDomainParameters(curves[i], points[i], n_s[i], h_s[i]);
83-
}
84-
85-
for (int i = 0; i < oids.length; i++)
86-
{
87-
oids[i] = new ASN1ObjectIdentifier(oidBase + i);
84+
DOMAIN_PARAMETERS[i] = new ECDomainParameters(curves[i], points[i], n_s[i], h_s[i]);
85+
OIDS[i] = OID_BASE.branch("" + i);
8886
}
8987
}
9088

@@ -94,7 +92,9 @@ private static ECPoint configureBasepoint(ECCurve curve, BigInteger x, BigIntege
9492
*/
9593
public static ASN1ObjectIdentifier[] getOIDs()
9694
{
97-
return oids;
95+
ASN1ObjectIdentifier[] result = new ASN1ObjectIdentifier[OIDS.length];
96+
System.arraycopy(OIDS, 0, result, 0, OIDS.length);
97+
return result;
9898
}
9999

100100
/**
@@ -103,11 +103,15 @@ public static ASN1ObjectIdentifier[] getOIDs()
103103
*/
104104
public static ECDomainParameters getByOID(ASN1ObjectIdentifier oid)
105105
{
106-
String oidStr = oid.getId();
107-
if (oidStr.startsWith(oidBase))
106+
if (oid.on(OID_BASE))
108107
{
109-
int index = Integer.parseInt(oidStr.substring(oidStr.lastIndexOf('.') + 1));
110-
return (index >= 0 && index < params.length) ? params[index] : null;
108+
for (int i = 0; i < 10; ++i)
109+
{
110+
if (OIDS[i].equals(oid))
111+
{
112+
return DOMAIN_PARAMETERS[i];
113+
}
114+
}
111115
}
112116
return null;
113117
}

core/src/main/java/org/bouncycastle/crypto/signers/DSTU4145Signer.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,21 @@ public void init(boolean forSigning, CipherParameters param)
3838
{
3939
if (forSigning)
4040
{
41+
SecureRandom providedRandom = null;
4142
if (param instanceof ParametersWithRandom)
4243
{
4344
ParametersWithRandom rParam = (ParametersWithRandom)param;
44-
45-
this.random = rParam.getRandom();
45+
providedRandom = rParam.getRandom();
4646
param = rParam.getParameters();
4747
}
48-
else
49-
{
50-
this.random = CryptoServicesRegistrar.getSecureRandom();
51-
}
5248

5349
this.key = (ECPrivateKeyParameters)param;
50+
this.random = CryptoServicesRegistrar.getSecureRandom(providedRandom);
5451
}
5552
else
5653
{
5754
this.key = (ECPublicKeyParameters)param;
55+
this.random = null;
5856
}
5957

6058
CryptoServicesRegistrar.checkConstraints(Utils.getDefaultProperties("DSTU4145", key, forSigning));

prov/src/test/java/org/bouncycastle/jce/provider/test/DSTU4145Test.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public String getName()
3636
public void performTest()
3737
throws Exception
3838
{
39-
40-
DSTU4145Test();
39+
implDSTU4145Test();
4140
generationTest();
4241
//parametersTest();
4342
generateFromCurveTest();
@@ -127,7 +126,7 @@ private void generateFromCurveTest()
127126
}
128127
}
129128

130-
private void DSTU4145Test()
129+
private void implDSTU4145Test()
131130
throws Exception
132131
{
133132

0 commit comments

Comments
 (0)