Skip to content

Commit 2aebe15

Browse files
committed
EC updates from bc-csharp
1 parent 13cfd02 commit 2aebe15

13 files changed

+108
-43
lines changed

core/src/main/java/org/bouncycastle/asn1/x9/X9IntegerConverter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ public class X9IntegerConverter
1717
* @param c the curve of interest.
1818
* @return the field size in bytes (rounded up).
1919
*/
20-
public int getByteLength(
21-
ECCurve c)
20+
public int getByteLength(ECCurve c)
2221
{
23-
return (c.getFieldSize() + 7) / 8;
22+
return c.getFieldElementEncodingLength();
2423
}
2524

2625
/**
@@ -29,10 +28,9 @@ public int getByteLength(
2928
* @param fe the field element of interest.
3029
* @return the field size in bytes (rounded up).
3130
*/
32-
public int getByteLength(
33-
ECFieldElement fe)
31+
public int getByteLength(ECFieldElement fe)
3432
{
35-
return (fe.getFieldSize() + 7) / 8;
33+
return fe.getEncodedLength();
3634
}
3735

3836
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void init(
4141

4242
public int getFieldSize()
4343
{
44-
return (key.getParameters().getCurve().getFieldSize() + 7) / 8;
44+
return key.getParameters().getCurve().getFieldElementEncodingLength();
4545
}
4646

4747
public BigInteger calculateAgreement(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void init(
4545

4646
public int getFieldSize()
4747
{
48-
return (key.getParameters().getCurve().getFieldSize() + 7) / 8;
48+
return key.getParameters().getCurve().getFieldElementEncodingLength();
4949
}
5050

5151
public BigInteger calculateAgreement(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void init(
2727

2828
public int getFieldSize()
2929
{
30-
return (key.getParameters().getCurve().getFieldSize() + 7) / 8;
30+
return key.getParameters().getCurve().getFieldElementEncodingLength();
3131
}
3232

3333
public AsymmetricKeyParameter calculateStage(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void init(
2525

2626
public int getFieldSize()
2727
{
28-
return (privParams.getStaticPrivateKey().getParameters().getCurve().getFieldSize() + 7) / 8;
28+
return privParams.getStaticPrivateKey().getParameters().getCurve().getFieldElementEncodingLength();
2929
}
3030

3131
public byte[] calculateAgreement(CipherParameters pubKey)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void init(
3131

3232
public int getFieldSize()
3333
{
34-
return (privParams.getStaticPrivateKey().getParameters().getCurve().getFieldSize() + 7) / 8;
34+
return privParams.getStaticPrivateKey().getParameters().getCurve().getFieldElementEncodingLength();
3535
}
3636

3737
public BigInteger calculateAgreement(CipherParameters pubKey)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public int getAgreementSize()
4848
*/
4949
public int getFieldSize()
5050
{
51-
return (key.getParameters().getCurve().getFieldSize() + 7) / 8;
51+
return key.getParameters().getCurve().getFieldElementEncodingLength();
5252
}
5353

5454
public byte[] calculateAgreement(CipherParameters pubKey)

core/src/main/java/org/bouncycastle/crypto/engines/SM2Engine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void init(boolean forEncryption, CipherParameters param)
9595
ecParams = ecKey.getParameters();
9696
}
9797

98-
curveLength = (ecParams.getCurve().getFieldSize() + 7) / 8;
98+
curveLength = ecParams.getCurve().getFieldElementEncodingLength();
9999

100100
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties("SM2", ConstraintUtils.bitsOfSecurityFor(ecParams.getCurve()), ecKey, Utils.getPurpose(forEncryption)));
101101
}

core/src/main/java/org/bouncycastle/crypto/kems/ECIESKEMExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,6 @@ public byte[] extractSecret(byte[] encapsulation)
119119

120120
public int getEncapsulationLength()
121121
{
122-
return (decKey.getParameters().getCurve().getFieldSize() / 8) * 2 + 1;
122+
return decKey.getParameters().getCurve().getAffinePointEncodingLength(false);
123123
}
124124
}

core/src/main/java/org/bouncycastle/crypto/parsers/ECIESPublicKeyParser.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.bouncycastle.crypto.parsers;
22

3+
import java.io.EOFException;
34
import java.io.IOException;
45
import java.io.InputStream;
56

67
import org.bouncycastle.crypto.KeyParser;
78
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
89
import org.bouncycastle.crypto.params.ECDomainParameters;
910
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
11+
import org.bouncycastle.math.ec.ECCurve;
1012
import org.bouncycastle.util.io.Streams;
1113

1214
public class ECIESPublicKeyParser
@@ -22,33 +24,45 @@ public ECIESPublicKeyParser(ECDomainParameters ecParams)
2224
public AsymmetricKeyParameter readKey(InputStream stream)
2325
throws IOException
2426
{
25-
byte[] V;
26-
int first = stream.read();
27+
int first = stream.read();
28+
if (first < 0)
29+
{
30+
throw new EOFException();
31+
}
2732

2833
// Decode the public ephemeral key
34+
boolean compressed;
2935
switch (first)
3036
{
3137
case 0x00: // infinity
3238
throw new IOException("Sender's public key invalid.");
3339

3440
case 0x02: // compressed
3541
case 0x03: // Byte length calculated as in ECPoint.getEncoded();
36-
V = new byte[1 + (ecParams.getCurve().getFieldSize()+7)/8];
42+
compressed = true;
3743
break;
3844

3945
case 0x04: // uncompressed or
4046
case 0x06: // hybrid
4147
case 0x07: // Byte length calculated as in ECPoint.getEncoded();
42-
V = new byte[1 + 2*((ecParams.getCurve().getFieldSize()+7)/8)];
48+
compressed = false;
4349
break;
4450

4551
default:
4652
throw new IOException("Sender's public key has invalid point encoding 0x" + Integer.toString(first, 16));
4753
}
4854

55+
ECCurve curve = ecParams.getCurve();
56+
int encodingLength = curve.getAffinePointEncodingLength(compressed);
57+
byte[] V = new byte[encodingLength];
4958
V[0] = (byte)first;
50-
Streams.readFully(stream, V, 1, V.length - 1);
5159

52-
return new ECPublicKeyParameters(ecParams.getCurve().decodePoint(V), ecParams);
60+
int readLength = encodingLength - 1;
61+
if (Streams.readFully(stream, V, 1, readLength) != readLength)
62+
{
63+
throw new EOFException();
64+
}
65+
66+
return new ECPublicKeyParameters(curve.decodePoint(V), ecParams);
5367
}
5468
}

0 commit comments

Comments
 (0)