Skip to content

Commit c114455

Browse files
committed
cleanup/compatibility changes
1 parent b781792 commit c114455

File tree

57 files changed

+1618
-1372
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1618
-1372
lines changed

ant/jdk14.xml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
<mkdir dir="${artifacts.dir}/checkstyle"/>
2424
<copy todir="${src.dir}">
2525
<fileset dir="core/src/main/java">
26-
<exclude name="**/ntru/**/*.java"/>
26+
<exclude name="**/legacy/**/ntru/**/*.java"/>
2727
<exclude name="**/xmss/**/*.java"/>
2828
<exclude name="**/lms/**/*.java"/>
29+
<exclude name="**/sike/*.java"/>
2930
</fileset>
3031
<fileset dir="util/src/main/java">
3132
<exclude name="**/oer/**/*.java"/>
@@ -66,11 +67,12 @@
6667
<exclude name="**/EC5*.java"/>
6768
<exclude name="**/PKCS12StoreParameter.java"/>
6869
<exclude name="**/JDKPKCS12StoreParameter.java"/>
69-
<exclude name="**/NTRU*.java"/>
70+
<exclude name="**/SIKE*.java"/>
71+
<exclude name="**/sike/*.java"/>
7072
<exclude name="**/XMS*.java"/>
7173
<exclude name="**/LMS*.java"/>
7274
<exclude name="**/IndexGenerator.java"/>
73-
<exclude name="**/ntru/**/*.java"/>
75+
<exclude name="**/legacy/**/ntru/**/*.java"/>
7476
<exclude name="**/xmss/**/*.java"/>
7577
<exclude name="**/lms/**/*.java"/>
7678
<exclude name="**/asymmetric/DSTU*.java"/>
@@ -86,15 +88,18 @@
8688
<exclude name="**/KDFFeed*.java"/>
8789
<exclude name="**/KDFDoublePipeline*.java"/>
8890
<exclude name="**/KDFCounterGener*.java"/>
89-
<exclude name="**/ntru/**/*.java"/>
91+
<exclude name="**/legacy/**/ntru/**/*.java"/>
92+
<exclude name="**/legacy/**/NTRU*.java"/>
9093
<exclude name="**/xmss/**/*.java"/>
9194
<exclude name="**/lms/**/*.java"/>
92-
<exclude name="**/NTRU*.java"/>
9395
<exclude name="**/pqc/**/EncryptionKeyTest.java"/>
9496
<exclude name="**/pqc/**/BitStringTest.java"/>
9597
<exclude name="**/pqc/**/XMS*.java"/>
9698
<exclude name="**/pqc/**/LMS*.java"/>
9799
<exclude name="**/pqc/**/HSS*.java"/>
100+
<exclude name="**/pqc/**/HRSS*.java"/>
101+
<exclude name="**/pqc/**/PolynomialTest.java"/>
102+
<exclude name="**/pqc/**/HPS*.java"/>
98103
<exclude name="**/GetInstanceTest.java"/>
99104
</fileset>
100105
<fileset dir="pg/src/test/java">
@@ -125,8 +130,7 @@
125130
<exclude name="**/MQVTest.java"/>
126131
<exclude name="**/pem/AllTests.java"/>
127132
<exclude name="**/AlgorithmParametersTest.java"/>
128-
<exclude name="**/ntru/**/*.java"/>
129-
<exclude name="**/NTRU*.java"/>
133+
<exclude name="**/legacy/**/ntru/**/*.java"/>
130134
<exclude name="**/crypto/engines/test/BitStringTest.java"/>
131135
<exclude name="**/crypto/engines/test/AllTests.java"/>
132136
<exclude name="**/crypto/signers/test/AllTests.java"/>
@@ -184,6 +188,7 @@
184188
<fileset dir="pkix/src/main/jdk1.4" includes="**/*.java"/>
185189
<fileset dir="pg/src/main/jdk1.4" includes="**/*.java"/>
186190
<fileset dir="tls/src/main/jdk1.4" includes="**/*.java"/>
191+
<fileset dir="util/src/main/jdk1.4" includes="**/*.java"/>
187192
<fileset dir="core/src/test/jdk1.4" includes="**/*.java"/>
188193
<fileset dir="mail/src/test/jdk1.4" includes="**/*.java"/>
189194
<fileset dir="prov/src/test/jdk1.4" includes="**/*.java"/>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.bouncycastle.crypto.kems;
22

33
import java.security.SecureRandom;
4-
import java.util.Arrays;
54

65
import org.bouncycastle.crypto.CipherParameters;
76
import org.bouncycastle.crypto.CryptoServicePurpose;
@@ -13,6 +12,7 @@
1312
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
1413
import org.bouncycastle.crypto.params.KeyParameter;
1514
import org.bouncycastle.crypto.params.RSAKeyParameters;
15+
import org.bouncycastle.util.Arrays;
1616

1717
/**
1818
* The RSA Key Encapsulation Mechanism (RSA-KEM) from ISO 18033-2.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public AsymmetricKeyParameter readKey(InputStream stream)
2727

2828
Streams.readFully(stream, V, 0, V.length);
2929

30-
return isX25519 ? new X25519PublicKeyParameters(V, 0) : new X448PublicKeyParameters(V, 0);
30+
// cast due to JVM compatibility
31+
return isX25519 ? (AsymmetricKeyParameter)new X25519PublicKeyParameters(V, 0) : (AsymmetricKeyParameter)new X448PublicKeyParameters(V, 0);
3132
}
3233
}
Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package org.bouncycastle.pqc.crypto.bike;
22

33
import java.security.SecureRandom;
4-
import java.util.concurrent.atomic.AtomicBoolean;
5-
6-
import javax.security.auth.DestroyFailedException;
74

85
import org.bouncycastle.crypto.EncapsulatedSecretGenerator;
96
import org.bouncycastle.crypto.SecretWithEncapsulation;
107
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
8+
import org.bouncycastle.pqc.crypto.util.SecretWithEncapsulationImpl;
119
import org.bouncycastle.util.Arrays;
1210

1311
public class BIKEKEMGenerator
@@ -38,56 +36,4 @@ public SecretWithEncapsulation generateEncapsulated(AsymmetricKeyParameter recip
3836

3937
return new SecretWithEncapsulationImpl(Arrays.copyOfRange(K, 0, key.getParameters().getSessionKeySize() / 8), cipherText);
4038
}
41-
42-
private static class SecretWithEncapsulationImpl
43-
implements SecretWithEncapsulation
44-
{
45-
private final AtomicBoolean hasBeenDestroyed = new AtomicBoolean(false);
46-
47-
private final byte[] sessionKey;
48-
private final byte[] cipher_text;
49-
50-
public SecretWithEncapsulationImpl(byte[] sessionKey, byte[] cipher_text)
51-
{
52-
this.sessionKey = sessionKey;
53-
this.cipher_text = cipher_text;
54-
}
55-
56-
public byte[] getSecret()
57-
{
58-
checkDestroyed();
59-
60-
return Arrays.clone(sessionKey);
61-
}
62-
63-
public byte[] getEncapsulation()
64-
{
65-
checkDestroyed();
66-
67-
return Arrays.clone(cipher_text);
68-
}
69-
70-
public void destroy()
71-
throws DestroyFailedException
72-
{
73-
if (!hasBeenDestroyed.getAndSet(true))
74-
{
75-
Arrays.clear(sessionKey);
76-
Arrays.clear(cipher_text);
77-
}
78-
}
79-
80-
public boolean isDestroyed()
81-
{
82-
return hasBeenDestroyed.get();
83-
}
84-
85-
void checkDestroyed()
86-
{
87-
if (isDestroyed())
88-
{
89-
throw new IllegalStateException("data has been destroyed");
90-
}
91-
}
92-
}
9339
}

core/src/main/java/org/bouncycastle/pqc/crypto/bike/Utils.java

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.bouncycastle.pqc.crypto.bike;
22

3-
import org.bouncycastle.util.Arrays;
4-
53
class Utils
64
{
75
static byte[] xorBytes(byte[] a, byte[] b, int size)
@@ -27,74 +25,52 @@ static int getHammingWeight(byte[] bytes)
2725

2826
static void fromByteArrayToBitArray(byte[] out, byte[] in)
2927
{
30-
byte[] tmpByte = Arrays.clone(in);
31-
String res = "";
32-
for (int i = 0; i < tmpByte.length; i++)
28+
int max = (out.length / 8);
29+
for (int i = 0; i < max; i++)
3330
{
34-
boolean negative = false;
35-
String tmp = "";
36-
tmp += Integer.toBinaryString(tmpByte[i] & 0xff);
37-
tmp = new StringBuilder(tmp).reverse().toString();
38-
if (tmpByte[i] != 0)
39-
{
40-
int t = tmpByte[i];
41-
}
42-
// padding with zeros
43-
int gap = 0;
44-
if (i == tmpByte.length - 1 && tmpByte.length % 8 != 0)
45-
{
46-
gap = out.length % 8 - tmp.length(); // last byte so it cannot parse to 8 bits
47-
}
48-
else
49-
{
50-
gap = 8 - tmp.length();
51-
}
52-
while (gap > 0)
31+
for (int j = 0; j != 8; j++)
5332
{
54-
tmp = tmp + '0';
55-
gap--;
33+
out[i * 8 + j] = (byte)((in[i] & (1 << j)) >>> j);
5634
}
57-
res += tmp;
5835
}
59-
fromBitStringToBytes(out, res);
60-
}
61-
62-
static void fromBitStringToBytes(byte[] out, String in)
63-
{
64-
for (int i = 0; i < out.length; i++)
36+
if (out.length % 8 != 0)
6537
{
66-
out[i] = (byte)(in.charAt(i) - '0');
38+
int off = max * 8;
39+
int count = 0;
40+
while (off < out.length)
41+
{
42+
out[off++] = (byte)((in[max] & (1 << count)) >>> count);
43+
count++;
44+
}
6745
}
6846
}
6947

7048
static void fromBitArrayToByteArray(byte[] out, byte[] in)
7149
{
7250
int count = 0;
7351
int pos = 0;
74-
byte[] tmpIn = Arrays.clone(in);
75-
long len = tmpIn.length;
52+
long len = in.length;
7653
while (count < len)
7754
{
78-
String tmp = "";
79-
80-
if (count + 8 >= tmpIn.length)
55+
if (count + 8 >= in.length)
8156
{// last set of bits cannot have enough 8 bits
82-
for (int j = tmpIn.length - count - 1; j >= 0; j--)
57+
int b = in[count];
58+
for (int j = in.length - count - 1; j >= 1; j--)
8359
{ //bin in reversed order
84-
tmp += tmpIn[count + j];
60+
b |= in[count + j] << j;
8561
}
62+
out[pos] = (byte)b;
8663
}
87-
8864
else
8965
{
90-
for (int j = 7; j >= 0; j--)
66+
int b = in[count];
67+
for (int j = 7; j >= 1; j--)
9168
{ //bin in reversed order
92-
tmp += tmpIn[count + j];
69+
b |= in[count + j] << j;
9370
}
71+
out[pos] = (byte)b;
9472
}
9573

96-
int b = Integer.parseInt(tmp, 2);
97-
out[pos] = (byte)b;
9874
count += 8;
9975
pos++;
10076
}

core/src/main/java/org/bouncycastle/pqc/crypto/crystals/dilithium/DilithiumEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.bouncycastle.pqc.crypto.crystals.dilithium;
22

33
import java.security.SecureRandom;
4-
import java.util.Arrays;
54

65
import org.bouncycastle.crypto.digests.SHAKEDigest;
6+
import org.bouncycastle.util.Arrays;
77

88
class DilithiumEngine
99
{

core/src/main/java/org/bouncycastle/pqc/crypto/crystals/dilithium/Ntt.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.bouncycastle.pqc.crypto.crystals.dilithium;
22

3-
import java.util.Arrays;
3+
import org.bouncycastle.util.Arrays;
44

55
class Ntt
66
{

core/src/main/java/org/bouncycastle/pqc/crypto/crystals/dilithium/Packing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.bouncycastle.pqc.crypto.crystals.dilithium;
22

3-
import java.util.Arrays;
3+
import org.bouncycastle.util.Arrays;
44

55
class Packing
66
{

core/src/main/java/org/bouncycastle/pqc/crypto/crystals/dilithium/Poly.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.bouncycastle.pqc.crypto.crystals.dilithium;
22

3-
import java.util.Arrays;
4-
53
import org.bouncycastle.crypto.digests.SHAKEDigest;
64

75
class Poly
@@ -786,9 +784,19 @@ public void shiftLeft()
786784
}
787785
}
788786

789-
@Override
790787
public String toString()
791788
{
792-
return Arrays.toString(coeffs);
789+
StringBuffer out = new StringBuffer();
790+
out.append("[");
791+
for (int i = 0; i < coeffs.length; i++)
792+
{
793+
out.append(coeffs[i]);
794+
if (i != coeffs.length - 1)
795+
{
796+
out.append(", ");
797+
}
798+
}
799+
out.append("]");
800+
return out.toString();
793801
}
794802
}

core/src/main/java/org/bouncycastle/pqc/crypto/crystals/dilithium/PolyVecK.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public String toString()
180180
String out = "[";
181181
for (int i = 0; i < dilithiumK; i++)
182182
{
183-
out += String.format("%d ", i) + this.getVectorIndex(i).toString();
183+
out += i + " " + this.getVectorIndex(i).toString();
184184
if (i == dilithiumK - 1)
185185
{
186186
continue;

0 commit comments

Comments
 (0)