Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fe5ccf2

Browse filesBrowse files
committed
Refactor to avoid allocations
1 parent 35a80b5 commit fe5ccf2
Copy full SHA for fe5ccf2

File tree

Expand file treeCollapse file tree

5 files changed

+32
-20
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+32
-20
lines changed
Open diff view settings
Collapse file

‎core/src/main/java/org/bouncycastle/asn1/ASN1OctetString.java‎

Copy file name to clipboardExpand all lines: core/src/main/java/org/bouncycastle/asn1/ASN1OctetString.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ public byte[] getOctets()
212212
return string;
213213
}
214214

215+
public int getOctetsLength()
216+
{
217+
return getOctets().length;
218+
}
219+
215220
public int hashCode()
216221
{
217222
return Arrays.hashCode(this.getOctets());
Collapse file

‎core/src/main/java/org/bouncycastle/asn1/pkcs/PrivateKeyInfo.java‎

Copy file name to clipboardExpand all lines: core/src/main/java/org/bouncycastle/asn1/pkcs/PrivateKeyInfo.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ public ASN1OctetString getPrivateKey()
195195
return new DEROctetString(privateKey.getOctets());
196196
}
197197

198+
public int getPrivateKeyLength()
199+
{
200+
return privateKey.getOctetsLength();
201+
}
202+
198203
public ASN1Encodable parsePrivateKey()
199204
throws IOException
200205
{
Collapse file

‎core/src/main/java/org/bouncycastle/crypto/util/PrivateKeyFactory.java‎

Copy file name to clipboardExpand all lines: core/src/main/java/org/bouncycastle/crypto/util/PrivateKeyFactory.java
+13-16Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,27 +185,23 @@ else if (algOID.equals(X9ObjectIdentifiers.id_ecPublicKey))
185185
}
186186
else if (algOID.equals(EdECObjectIdentifiers.id_X25519))
187187
{
188-
byte[] encoding = keyInfo.getPrivateKey().getOctets();
189-
190-
// Java 11 bug: exact length of X25519/X448 secret used in Java 11
191-
if (encoding.length != X25519PrivateKeyParameters.KEY_SIZE)
192-
{
193-
encoding = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
194-
}
188+
// Java 11 bug: exact length of X25519/X448 secret used in Java 11
189+
if (X25519PrivateKeyParameters.KEY_SIZE == keyInfo.getPrivateKeyLength())
190+
{
191+
return new X25519PrivateKeyParameters(keyInfo.getPrivateKey().getOctets());
192+
}
195193

196-
return new X25519PrivateKeyParameters(encoding);
194+
return new X25519PrivateKeyParameters(getRawKey(keyInfo));
197195
}
198196
else if (algOID.equals(EdECObjectIdentifiers.id_X448))
199197
{
200-
byte[] encoding = keyInfo.getPrivateKey().getOctets();
201-
202198
// Java 11 bug: exact length of X25519/X448 secret used in Java 11
203-
if (encoding.length != X448PrivateKeyParameters.KEY_SIZE)
199+
if (X448PrivateKeyParameters.KEY_SIZE == keyInfo.getPrivateKeyLength())
204200
{
205-
encoding = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
201+
return new X448PrivateKeyParameters(keyInfo.getPrivateKey().getOctets());
206202
}
207203

208-
return new X448PrivateKeyParameters(encoding);
204+
return new X448PrivateKeyParameters(getRawKey(keyInfo));
209205
}
210206
else if (algOID.equals(EdECObjectIdentifiers.id_Ed25519))
211207
{
@@ -235,11 +231,12 @@ else if (
235231
gostParams.getPublicKeyParamSet(),
236232
gostParams.getDigestParamSet(),
237233
gostParams.getEncryptionParamSet());
238-
ASN1OctetString privEnc = keyInfo.getPrivateKey();
239234

240-
if (privEnc.getOctets().length == 32 || privEnc.getOctets().length == 64)
235+
int privateKeyLength = keyInfo.getPrivateKeyLength();
236+
237+
if (privateKeyLength == 32 || privateKeyLength == 64)
241238
{
242-
d = new BigInteger(1, Arrays.reverse(privEnc.getOctets()));
239+
d = new BigInteger(1, Arrays.reverse(keyInfo.getPrivateKey().getOctets()));
243240
}
244241
else
245242
{
Collapse file

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/COMPOSITE.java‎

Copy file name to clipboardExpand all lines: prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/COMPOSITE.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public CompositeKeyInfoConverter(ConfigurableProvider provider)
8484
public PrivateKey generatePrivate(PrivateKeyInfo keyInfo)
8585
throws IOException
8686
{
87-
ASN1Sequence keySeq = ASN1Sequence.getInstance(keyInfo.getPrivateKey().getOctets());
87+
ASN1Sequence keySeq = ASN1Sequence.getInstance(keyInfo.parsePrivateKey());
8888
PrivateKey[] privKeys = new PrivateKey[keySeq.size()];
8989

9090
for (int i = 0; i != keySeq.size(); i++)
Collapse file

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/edec/BCXDHPrivateKey.java‎

Copy file name to clipboardExpand all lines: prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/edec/BCXDHPrivateKey.java
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,16 @@ public class BCXDHPrivateKey
6060
private void populateFromPrivateKeyInfo(PrivateKeyInfo keyInfo)
6161
throws IOException
6262
{
63-
byte[] encoding = keyInfo.getPrivateKey().getOctets();
63+
int privateKeyLength = keyInfo.getPrivateKeyLength();
64+
byte[] encoding;
6465

6566
// exact length of X25519/X448 secret used in Java 11
66-
if (encoding.length != X25519PrivateKeyParameters.KEY_SIZE &&
67-
encoding.length != X448PrivateKeyParameters.KEY_SIZE)
67+
if (privateKeyLength == X25519PrivateKeyParameters.KEY_SIZE ||
68+
privateKeyLength == X448PrivateKeyParameters.KEY_SIZE)
69+
{
70+
encoding = keyInfo.getPrivateKey().getOctets();
71+
}
72+
else
6873
{
6974
encoding = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
7075
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.