Skip to content

Commit 0a9df8c

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/master'
2 parents f90e150 + 9dd15a0 commit 0a9df8c

File tree

11 files changed

+50
-263
lines changed

11 files changed

+50
-263
lines changed

core/src/main/java/org/bouncycastle/crypto/agreement/ECVKOAgreement.java

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.bouncycastle.crypto.params.ParametersWithUKM;
1212
import org.bouncycastle.math.ec.ECAlgorithms;
1313
import org.bouncycastle.math.ec.ECPoint;
14-
import org.bouncycastle.util.BigIntegers;
14+
import org.bouncycastle.util.Arrays;
1515

1616
/**
1717
* GOST VKO key agreement class - RFC 7836 Section 4.3
@@ -28,24 +28,30 @@ public ECVKOAgreement(Digest digest)
2828
this.digest = digest;
2929
}
3030

31-
public void init(
32-
CipherParameters key)
31+
public void init(CipherParameters key)
3332
{
3433
ParametersWithUKM p = (ParametersWithUKM)key;
3534

3635
this.key = (ECPrivateKeyParameters)p.getParameters();
37-
this.ukm = toInteger(p.getUKM());
36+
this.ukm = new BigInteger(1, Arrays.reverse(p.getUKM()));
3837

3938
CryptoServicesRegistrar.checkConstraints(Utils.getDefaultProperties("ECVKO", this.key));
4039
}
4140

41+
public int getAgreementSize()
42+
{
43+
return digest.getDigestSize();
44+
}
45+
46+
/**
47+
* @deprecated Will be removed
48+
*/
4249
public int getFieldSize()
4350
{
4451
return (key.getParameters().getCurve().getFieldSize() + 7) / 8;
4552
}
4653

47-
public byte[] calculateAgreement(
48-
CipherParameters pubKey)
54+
public byte[] calculateAgreement(CipherParameters pubKey)
4955
{
5056
ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey;
5157
ECDomainParameters params = key.getParameters();
@@ -60,7 +66,7 @@ public byte[] calculateAgreement(
6066
ECPoint pubPoint = ECAlgorithms.cleanPoint(params.getCurve(), pub.getQ());
6167
if (pubPoint.isInfinity())
6268
{
63-
throw new IllegalStateException("Infinity is not a valid public key for ECDHC");
69+
throw new IllegalStateException("Infinity is not a valid public key for ECVKO");
6470
}
6571

6672
ECPoint P = pubPoint.multiply(hd).normalize();
@@ -70,55 +76,16 @@ public byte[] calculateAgreement(
7076
throw new IllegalStateException("Infinity is not a valid agreement value for ECVKO");
7177
}
7278

73-
return fromPoint(P);
74-
}
75-
76-
private static BigInteger toInteger(byte[] ukm)
77-
{
78-
byte[] v = new byte[ukm.length];
79-
80-
for (int i = 0; i != v.length; i++)
81-
{
82-
v[i] = ukm[ukm.length - i - 1];
83-
}
84-
85-
return new BigInteger(1, v);
86-
}
87-
88-
private byte[] fromPoint(ECPoint v)
89-
{
90-
BigInteger bX = v.getAffineXCoord().toBigInteger();
91-
BigInteger bY = v.getAffineYCoord().toBigInteger();
92-
93-
int size;
94-
if (bX.toByteArray().length > 33)
95-
{
96-
size = 64;
97-
}
98-
else
99-
{
100-
size = 32;
101-
}
102-
103-
byte[] bytes = new byte[2 * size];
104-
byte[] x = BigIntegers.asUnsignedByteArray(size, bX);
105-
byte[] y = BigIntegers.asUnsignedByteArray(size, bY);
79+
byte[] encoding = P.getEncoded(false);
80+
int encodingLength = encoding.length;
81+
int feSize = encodingLength / 2;
10682

107-
for (int i = 0; i != size; i++)
108-
{
109-
bytes[i] = x[size - i - 1];
110-
}
111-
for (int i = 0; i != size; i++)
112-
{
113-
bytes[size + i] = y[size - i - 1];
114-
}
115-
116-
digest.update(bytes, 0, bytes.length);
83+
Arrays.reverseInPlace(encoding, encodingLength - feSize * 2, feSize);
84+
Arrays.reverseInPlace(encoding, encodingLength - feSize , feSize);
11785

11886
byte[] rv = new byte[digest.getDigestSize()];
119-
87+
digest.update(encoding, encodingLength - feSize * 2, feSize * 2);
12088
digest.doFinal(rv, 0);
121-
12289
return rv;
12390
}
12491
}

core/src/main/java/org/bouncycastle/crypto/params/ParametersWithUKM.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public ParametersWithUKM(
1818
public ParametersWithUKM(
1919
CipherParameters parameters,
2020
byte[] ukm,
21-
int ivOff,
22-
int ivLen)
21+
int ukmOff,
22+
int ukmLen)
2323
{
24-
this.ukm = new byte[ivLen];
24+
this.ukm = new byte[ukmLen];
2525
this.parameters = parameters;
2626

27-
System.arraycopy(ukm, ivOff, this.ukm, 0, ivLen);
27+
System.arraycopy(ukm, ukmOff, this.ukm, 0, ukmLen);
2828
}
2929

3030
public byte[] getUKM()
Lines changed: 3 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,11 @@
11
package org.bouncycastle.crypto.signers;
22

3-
import java.math.BigInteger;
4-
import java.security.SecureRandom;
5-
6-
import org.bouncycastle.crypto.CipherParameters;
7-
import org.bouncycastle.crypto.CryptoServicesRegistrar;
8-
import org.bouncycastle.crypto.DSAExt;
9-
import org.bouncycastle.crypto.params.ECDomainParameters;
10-
import org.bouncycastle.crypto.params.ECKeyParameters;
11-
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
12-
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
13-
import org.bouncycastle.crypto.params.ParametersWithRandom;
14-
import org.bouncycastle.math.ec.ECAlgorithms;
15-
import org.bouncycastle.math.ec.ECConstants;
16-
import org.bouncycastle.math.ec.ECMultiplier;
17-
import org.bouncycastle.math.ec.ECPoint;
18-
import org.bouncycastle.math.ec.FixedPointCombMultiplier;
19-
import org.bouncycastle.util.Arrays;
20-
import org.bouncycastle.util.BigIntegers;
21-
223
/**
234
* GOST R 34.10-2012 Signature Algorithm
5+
*
6+
* @deprecated Use {@link ECGOST3410Signer} instead.
247
*/
258
public class ECGOST3410_2012Signer
26-
implements DSAExt
9+
extends ECGOST3410Signer
2710
{
28-
ECKeyParameters key;
29-
30-
SecureRandom random;
31-
32-
public void init(
33-
boolean forSigning,
34-
CipherParameters param)
35-
{
36-
if (forSigning)
37-
{
38-
if (param instanceof ParametersWithRandom)
39-
{
40-
ParametersWithRandom rParam = (ParametersWithRandom)param;
41-
42-
this.random = rParam.getRandom();
43-
this.key = (ECPrivateKeyParameters)rParam.getParameters();
44-
}
45-
else
46-
{
47-
this.random = CryptoServicesRegistrar.getSecureRandom();
48-
this.key = (ECPrivateKeyParameters)param;
49-
}
50-
}
51-
else
52-
{
53-
this.key = (ECPublicKeyParameters)param;
54-
}
55-
56-
CryptoServicesRegistrar.checkConstraints(Utils.getDefaultProperties("ECGOST3410_2012", key, forSigning));
57-
}
58-
59-
public BigInteger getOrder()
60-
{
61-
return key.getParameters().getN();
62-
}
63-
64-
/**
65-
* generate a signature for the given message using the key we were
66-
* initialised with. For conventional GOST3410 2012 the message should be a GOST3411 2012
67-
* hash of the message of interest.
68-
*
69-
* @param message the message that will be verified later.
70-
*/
71-
public BigInteger[] generateSignature(
72-
byte[] message)
73-
{
74-
byte[] mRev = Arrays.reverse(message); // conversion is little-endian
75-
BigInteger e = new BigInteger(1, mRev);
76-
77-
ECDomainParameters ec = key.getParameters();
78-
BigInteger n = ec.getN();
79-
BigInteger d = ((ECPrivateKeyParameters)key).getD();
80-
81-
BigInteger r, s;
82-
83-
ECMultiplier basePointMultiplier = createBasePointMultiplier();
84-
85-
do // generate s
86-
{
87-
BigInteger k;
88-
do // generate r
89-
{
90-
do
91-
{
92-
k = BigIntegers.createRandomBigInteger(n.bitLength(), random);
93-
}
94-
while (k.equals(ECConstants.ZERO));
95-
96-
ECPoint p = basePointMultiplier.multiply(ec.getG(), k).normalize();
97-
98-
r = p.getAffineXCoord().toBigInteger().mod(n);
99-
}
100-
while (r.equals(ECConstants.ZERO));
101-
102-
s = (k.multiply(e)).add(d.multiply(r)).mod(n);
103-
}
104-
while (s.equals(ECConstants.ZERO));
105-
106-
return new BigInteger[]{ r, s };
107-
}
108-
109-
/**
110-
* return true if the value r and s represent a GOST3410 2012 signature for
111-
* the passed in message (for standard GOST3410 2012 the message should be
112-
* a GOST3411 2012 hash of the real message to be verified).
113-
*/
114-
public boolean verifySignature(
115-
byte[] message,
116-
BigInteger r,
117-
BigInteger s)
118-
{
119-
byte[] mRev = Arrays.reverse(message); // conversion is little-endian
120-
BigInteger e = new BigInteger(1, mRev);
121-
BigInteger n = key.getParameters().getN();
122-
123-
// r in the range [1,n-1]
124-
if (r.compareTo(ECConstants.ONE) < 0 || r.compareTo(n) >= 0)
125-
{
126-
return false;
127-
}
128-
129-
// s in the range [1,n-1]
130-
if (s.compareTo(ECConstants.ONE) < 0 || s.compareTo(n) >= 0)
131-
{
132-
return false;
133-
}
134-
135-
BigInteger v = BigIntegers.modOddInverseVar(n, e);
136-
137-
BigInteger z1 = s.multiply(v).mod(n);
138-
BigInteger z2 = (n.subtract(r)).multiply(v).mod(n);
139-
140-
ECPoint G = key.getParameters().getG(); // P
141-
ECPoint Q = ((ECPublicKeyParameters)key).getQ();
142-
143-
ECPoint point = ECAlgorithms.sumOfTwoMultiplies(G, z1, Q, z2).normalize();
144-
145-
// components must be bogus.
146-
if (point.isInfinity())
147-
{
148-
return false;
149-
}
150-
151-
BigInteger R = point.getAffineXCoord().toBigInteger().mod(n);
152-
153-
return R.equals(r);
154-
}
155-
156-
protected ECMultiplier createBasePointMultiplier()
157-
{
158-
return new FixedPointCombMultiplier();
159-
}
16011
}

core/src/main/java/org/bouncycastle/util/Arrays.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,17 @@ public static byte[] reverseInPlace(byte[] a)
10901090
return a;
10911091
}
10921092

1093+
public static void reverseInPlace(byte[] a, int aOff, int aLen)
1094+
{
1095+
int p1 = aOff, p2 = aOff + aLen - 1;
1096+
while (p1 < p2)
1097+
{
1098+
byte t1 = a[p1], t2 = a[p2];
1099+
a[p1++] = t2;
1100+
a[p2--] = t1;
1101+
}
1102+
}
1103+
10931104
public static int[] reverseInPlace(int[] a)
10941105
{
10951106
if (null == a)

core/src/test/java/org/bouncycastle/crypto/test/AsymmetricConstraintsTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
import org.bouncycastle.crypto.signers.DSTU4145Signer;
9696
import org.bouncycastle.crypto.signers.ECDSASigner;
9797
import org.bouncycastle.crypto.signers.ECGOST3410Signer;
98-
import org.bouncycastle.crypto.signers.ECGOST3410_2012Signer;
9998
import org.bouncycastle.crypto.signers.Ed25519Signer;
10099
import org.bouncycastle.crypto.signers.Ed25519ctxSigner;
101100
import org.bouncycastle.crypto.signers.Ed25519phSigner;
@@ -389,7 +388,6 @@ private void testEC()
389388
// first though.
390389
ecSignerTest(kp.getPublic(), kp.getPrivate(), new ECDSASigner());
391390
ecSignerTest(kp.getPublic(), kp.getPrivate(), new DSTU4145Signer());
392-
ecSignerTest(kp.getPublic(), kp.getPrivate(), new ECGOST3410_2012Signer());
393391
ecSignerTest(kp.getPublic(), kp.getPrivate(), new ECGOST3410Signer());
394392
ecSignerTest(kp.getPublic(), kp.getPrivate(), new SM2Signer());
395393

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ECGOST.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public void configure(ConfigurableProvider provider)
6666
"ECGOST3410-2012",
6767
new org.bouncycastle.jcajce.provider.asymmetric.ecgost12.KeyFactorySpi());
6868
registerOid(provider, RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_256,
69-
"ECGOST3410-2012",
70-
new org.bouncycastle.jcajce.provider.asymmetric.ecgost12.KeyFactorySpi());
69+
"ECGOST3410-2012",
70+
new org.bouncycastle.jcajce.provider.asymmetric.ecgost12.KeyFactorySpi());
7171
registerOidAlgorithmParameters(provider,
7272
RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256, "ECGOST3410-2012");
7373

@@ -82,17 +82,13 @@ public void configure(ConfigurableProvider provider)
8282

8383
provider.addAlgorithm("KeyPairGenerator.ECGOST3410-2012",
8484
PREFIX_GOST_2012 + "KeyPairGeneratorSpi");
85-
provider.addAlgorithm("Alg.Alias.KeyPairGenerator.ECGOST3410-2012",
86-
"ECGOST3410-2012");
8785
provider.addAlgorithm("Alg.Alias.KeyPairGenerator.GOST-3410-2012",
8886
"ECGOST3410-2012");
8987

9088
// 256 signature
9189

9290
provider.addAlgorithm("Signature.ECGOST3410-2012-256",
9391
PREFIX_GOST_2012 + "ECGOST2012SignatureSpi256");
94-
provider.addAlgorithm("Alg.Alias.Signature.ECGOST3410-2012-256",
95-
"ECGOST3410-2012-256");
9692
provider.addAlgorithm("Alg.Alias.Signature.GOST-3410-2012-256",
9793
"ECGOST3410-2012-256");
9894
provider.addAlgorithm("Alg.Alias.Signature.GOST3411WITHECGOST3410-2012-256",
@@ -104,7 +100,6 @@ public void configure(ConfigurableProvider provider)
104100

105101
// 512 signature
106102

107-
108103
provider.addAlgorithm("Signature.ECGOST3410-2012-512",
109104
PREFIX_GOST_2012 + "ECGOST2012SignatureSpi512");
110105
provider.addAlgorithm("Alg.Alias.Signature.ECGOST3410-2012-512",
@@ -113,11 +108,13 @@ public void configure(ConfigurableProvider provider)
113108
"ECGOST3410-2012-512");
114109
provider.addAlgorithm("Alg.Alias.Signature.GOST3411WITHECGOST3410-2012-512",
115110
"ECGOST3410-2012-512");
116-
111+
117112
addSignatureAlgorithm(provider, "GOST3411-2012-512", "ECGOST3410-2012-512",
118113
PREFIX_GOST_2012 + "ECGOST2012SignatureSpi512",
119114
RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512);
120115

116+
// KeyAgreement
117+
121118
provider.addAlgorithm("KeyAgreement.ECGOST3410-2012-256", PREFIX_GOST_2012 + "KeyAgreementSpi$ECVKO256");
122119
provider.addAlgorithm("KeyAgreement.ECGOST3410-2012-512", PREFIX_GOST_2012 + "KeyAgreementSpi$ECVKO512");
123120

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/GOST.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void configure(ConfigurableProvider provider)
3939
provider.addAlgorithm("Alg.Alias.Signature.GOST3411withGOST3410", "GOST3410");
4040
provider.addAlgorithm("Alg.Alias.Signature.GOST3411WITHGOST3410", "GOST3410");
4141
provider.addAlgorithm("Alg.Alias.Signature.GOST3411WithGOST3410", "GOST3410");
42+
provider.addAlgorithm("Alg.Alias.Signature.GOST3411/GOST3410", "GOST3410");
4243
provider.addAlgorithm("Alg.Alias.Signature." + CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_94, "GOST3410");
4344

4445

0 commit comments

Comments
 (0)