Skip to content

Commit 420d65e

Browse files
author
gefeili
committed
Merge remote-tracking branch 'origin/master'
2 parents 9e1a4bd + d61be28 commit 420d65e

File tree

36 files changed

+3047
-1254
lines changed

36 files changed

+3047
-1254
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.bouncycastle.crypto;
2+
3+
import org.bouncycastle.crypto.digests.EncodableDigest;
4+
import org.bouncycastle.util.Memoable;
5+
6+
/**
7+
* Extended digest which provides the ability to store state and
8+
* provide an encoding.
9+
*/
10+
public interface SavableDigest
11+
extends ExtendedDigest, EncodableDigest, Memoable
12+
{
13+
}

core/src/main/java/org/bouncycastle/crypto/digests/SHA256Digest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.bouncycastle.crypto.CryptoServiceProperties;
55
import org.bouncycastle.crypto.CryptoServicePurpose;
66
import org.bouncycastle.crypto.CryptoServicesRegistrar;
7+
import org.bouncycastle.crypto.SavableDigest;
78
import org.bouncycastle.util.Memoable;
89
import org.bouncycastle.util.Pack;
910

@@ -21,7 +22,7 @@
2122
*/
2223
public class SHA256Digest
2324
extends GeneralDigest
24-
implements EncodableDigest
25+
implements SavableDigest
2526
{
2627
private static final int DIGEST_LENGTH = 32;
2728

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ public byte[] wrap(byte[] in, int inOff, int inLen)
178178
System.arraycopy(this.iv, 0, TEMP2, 0, this.iv.length);
179179
System.arraycopy(TEMP1, 0, TEMP2, this.iv.length, TEMP1.length);
180180

181-
// Reverse the order of the octets in TEMP2 and call the result TEMP3.
182-
byte[] TEMP3 = reverse(TEMP2);
181+
// Reverse the order of the octets in TEMP2.
182+
Arrays.reverseInPlace(TEMP2);
183183

184184
// Encrypt TEMP3 in CBC mode using the KEK and an initialization vector
185185
// of 0x 4a dd a2 2c 79 e8 21 05. The resulting cipher text is the desired
@@ -188,12 +188,12 @@ public byte[] wrap(byte[] in, int inOff, int inLen)
188188

189189
this.engine.init(true, param2);
190190

191-
for (int currentBytePos = 0; currentBytePos != TEMP3.length; currentBytePos += blockSize)
191+
for (int currentBytePos = 0; currentBytePos != TEMP2.length; currentBytePos += blockSize)
192192
{
193-
engine.processBlock(TEMP3, currentBytePos, TEMP3, currentBytePos);
193+
engine.processBlock(TEMP2, currentBytePos, TEMP2, currentBytePos);
194194
}
195195

196-
return TEMP3;
196+
return TEMP2;
197197
}
198198

199199
/**
@@ -246,15 +246,15 @@ public byte[] unwrap(byte[] in, int inOff, int inLen)
246246

247247
this.engine.init(false, param2);
248248

249-
byte TEMP3[] = new byte[inLen];
249+
byte TEMP2[] = new byte[inLen];
250250

251251
for (int currentBytePos = 0; currentBytePos != inLen; currentBytePos += blockSize)
252252
{
253-
engine.processBlock(in, inOff + currentBytePos, TEMP3, currentBytePos);
253+
engine.processBlock(in, inOff + currentBytePos, TEMP2, currentBytePos);
254254
}
255255

256-
// Reverse the order of the octets in TEMP3 and call the result TEMP2.
257-
byte[] TEMP2 = reverse(TEMP3);
256+
// Reverse the order of the octets in TEMP2.
257+
Arrays.reverseInPlace(TEMP2);
258258

259259
// Decompose TEMP2 into IV, the first 8 octets, and TEMP1, the remaining octets.
260260
this.iv = new byte[8];
@@ -337,14 +337,4 @@ private boolean checkCMSKeyChecksum(
337337
{
338338
return Arrays.constantTimeAreEqual(calculateCMSKeyChecksum(key), checksum);
339339
}
340-
341-
private static byte[] reverse(byte[] bs)
342-
{
343-
byte[] result = new byte[bs.length];
344-
for (int i = 0; i < bs.length; i++)
345-
{
346-
result[i] = bs[bs.length - (i + 1)];
347-
}
348-
return result;
349-
}
350340
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ public Ed25519PublicKeyParameters generatePublicKey()
6464
{
6565
if (null == cachedPublicKey)
6666
{
67-
byte[] publicKey = new byte[Ed25519.PUBLIC_KEY_SIZE];
68-
Ed25519.generatePublicKey(data, 0, publicKey, 0);
69-
cachedPublicKey = new Ed25519PublicKeyParameters(publicKey, 0);
67+
cachedPublicKey = new Ed25519PublicKeyParameters(Ed25519.generatePublicKey(data, 0));
7068
}
7169

7270
return cachedPublicKey;

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

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import java.io.InputStream;
66

77
import org.bouncycastle.math.ec.rfc8032.Ed25519;
8-
import org.bouncycastle.util.Arrays;
98
import org.bouncycastle.util.io.Streams;
109

1110
public final class Ed25519PublicKeyParameters
1211
extends AsymmetricKeyParameter
1312
{
1413
public static final int KEY_SIZE = Ed25519.PUBLIC_KEY_SIZE;
1514

16-
private final byte[] data = new byte[KEY_SIZE];
15+
private final Ed25519.PublicPoint publicPoint;
1716

1817
public Ed25519PublicKeyParameters(byte[] buf)
1918
{
@@ -24,27 +23,45 @@ public Ed25519PublicKeyParameters(byte[] buf, int off)
2423
{
2524
super(false);
2625

27-
System.arraycopy(buf, off, data, 0, KEY_SIZE);
26+
this.publicPoint = parse(buf, off);
2827
}
2928

3029
public Ed25519PublicKeyParameters(InputStream input) throws IOException
3130
{
3231
super(false);
3332

33+
byte[] data = new byte[KEY_SIZE];
34+
3435
if (KEY_SIZE != Streams.readFully(input, data))
3536
{
3637
throw new EOFException("EOF encountered in middle of Ed25519 public key");
3738
}
39+
40+
this.publicPoint = parse(data, 0);
41+
}
42+
43+
public Ed25519PublicKeyParameters(Ed25519.PublicPoint publicPoint)
44+
{
45+
super(false);
46+
47+
if (publicPoint == null)
48+
{
49+
throw new NullPointerException("'publicPoint' cannot be null");
50+
}
51+
52+
this.publicPoint = publicPoint;
3853
}
3954

4055
public void encode(byte[] buf, int off)
4156
{
42-
System.arraycopy(data, 0, buf, off, KEY_SIZE);
57+
Ed25519.encodePublicPoint(publicPoint, buf, off);
4358
}
4459

4560
public byte[] getEncoded()
4661
{
47-
return Arrays.clone(data);
62+
byte[] data = new byte[KEY_SIZE];
63+
encode(data, 0);
64+
return data;
4865
}
4966

5067
public boolean verify(int algorithm, byte[] ctx, byte[] msg, int msgOff, int msgLen, byte[] sig, int sigOff)
@@ -58,7 +75,7 @@ public boolean verify(int algorithm, byte[] ctx, byte[] msg, int msgOff, int msg
5875
throw new IllegalArgumentException("ctx");
5976
}
6077

61-
return Ed25519.verify(sig, sigOff, data, 0, msg, msgOff, msgLen);
78+
return Ed25519.verify(sig, sigOff, publicPoint, msg, msgOff, msgLen);
6279
}
6380
case Ed25519.Algorithm.Ed25519ctx:
6481
{
@@ -71,7 +88,7 @@ public boolean verify(int algorithm, byte[] ctx, byte[] msg, int msgOff, int msg
7188
throw new IllegalArgumentException("ctx");
7289
}
7390

74-
return Ed25519.verify(sig, sigOff, data, 0, ctx, msg, msgOff, msgLen);
91+
return Ed25519.verify(sig, sigOff, publicPoint, ctx, msg, msgOff, msgLen);
7592
}
7693
case Ed25519.Algorithm.Ed25519ph:
7794
{
@@ -88,7 +105,7 @@ public boolean verify(int algorithm, byte[] ctx, byte[] msg, int msgOff, int msg
88105
throw new IllegalArgumentException("msgLen");
89106
}
90107

91-
return Ed25519.verifyPrehash(sig, sigOff, data, 0, ctx, msg, msgOff);
108+
return Ed25519.verifyPrehash(sig, sigOff, publicPoint, ctx, msg, msgOff);
92109
}
93110
default:
94111
{
@@ -97,6 +114,16 @@ public boolean verify(int algorithm, byte[] ctx, byte[] msg, int msgOff, int msg
97114
}
98115
}
99116

117+
private static Ed25519.PublicPoint parse(byte[] buf, int off)
118+
{
119+
Ed25519.PublicPoint publicPoint = Ed25519.validatePublicKeyPartialExport(buf, off);
120+
if (publicPoint == null)
121+
{
122+
throw new IllegalArgumentException("invalid public key");
123+
}
124+
return publicPoint;
125+
}
126+
100127
private static byte[] validate(byte[] buf)
101128
{
102129
if (buf.length != KEY_SIZE)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ public Ed448PublicKeyParameters generatePublicKey()
6464
{
6565
if (null == cachedPublicKey)
6666
{
67-
byte[] publicKey = new byte[Ed448.PUBLIC_KEY_SIZE];
68-
Ed448.generatePublicKey(data, 0, publicKey, 0);
69-
cachedPublicKey = new Ed448PublicKeyParameters(publicKey, 0);
67+
cachedPublicKey = new Ed448PublicKeyParameters(Ed448.generatePublicKey(data, 0));
7068
}
7169

7270
return cachedPublicKey;

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import java.io.InputStream;
66

77
import org.bouncycastle.math.ec.rfc8032.Ed448;
8-
import org.bouncycastle.util.Arrays;
98
import org.bouncycastle.util.io.Streams;
109

1110
public final class Ed448PublicKeyParameters
1211
extends AsymmetricKeyParameter
1312
{
1413
public static final int KEY_SIZE = Ed448.PUBLIC_KEY_SIZE;
1514

16-
private final byte[] data = new byte[KEY_SIZE];
15+
private final Ed448.PublicPoint publicPoint;
1716

1817
public Ed448PublicKeyParameters(byte[] buf)
1918
{
@@ -24,27 +23,45 @@ public Ed448PublicKeyParameters(byte[] buf, int off)
2423
{
2524
super(false);
2625

27-
System.arraycopy(buf, off, data, 0, KEY_SIZE);
26+
this.publicPoint = parse(buf, off);
2827
}
2928

3029
public Ed448PublicKeyParameters(InputStream input) throws IOException
3130
{
3231
super(false);
3332

33+
byte[] data = new byte[KEY_SIZE];
34+
3435
if (KEY_SIZE != Streams.readFully(input, data))
3536
{
3637
throw new EOFException("EOF encountered in middle of Ed448 public key");
3738
}
39+
40+
this.publicPoint = parse(data, 0);
41+
}
42+
43+
public Ed448PublicKeyParameters(Ed448.PublicPoint publicPoint)
44+
{
45+
super(false);
46+
47+
if (publicPoint == null)
48+
{
49+
throw new NullPointerException("'publicPoint' cannot be null");
50+
}
51+
52+
this.publicPoint = publicPoint;
3853
}
3954

4055
public void encode(byte[] buf, int off)
4156
{
42-
System.arraycopy(data, 0, buf, off, KEY_SIZE);
57+
Ed448.encodePublicPoint(publicPoint, buf, off);
4358
}
4459

4560
public byte[] getEncoded()
4661
{
47-
return Arrays.clone(data);
62+
byte[] data = new byte[KEY_SIZE];
63+
encode(data, 0);
64+
return data;
4865
}
4966

5067
public boolean verify(int algorithm, byte[] ctx, byte[] msg, int msgOff, int msgLen, byte[] sig, int sigOff)
@@ -62,7 +79,7 @@ public boolean verify(int algorithm, byte[] ctx, byte[] msg, int msgOff, int msg
6279
throw new IllegalArgumentException("ctx");
6380
}
6481

65-
return Ed448.verify(sig, sigOff, data, 0, ctx, msg, msgOff, msgLen);
82+
return Ed448.verify(sig, sigOff, publicPoint, ctx, msg, msgOff, msgLen);
6683
}
6784
case Ed448.Algorithm.Ed448ph:
6885
{
@@ -79,7 +96,7 @@ public boolean verify(int algorithm, byte[] ctx, byte[] msg, int msgOff, int msg
7996
throw new IllegalArgumentException("msgLen");
8097
}
8198

82-
return Ed448.verifyPrehash(sig, sigOff, data, 0, ctx, msg, msgOff);
99+
return Ed448.verifyPrehash(sig, sigOff, publicPoint, ctx, msg, msgOff);
83100
}
84101
default:
85102
{
@@ -88,6 +105,16 @@ public boolean verify(int algorithm, byte[] ctx, byte[] msg, int msgOff, int msg
88105
}
89106
}
90107

108+
private static Ed448.PublicPoint parse(byte[] buf, int off)
109+
{
110+
Ed448.PublicPoint publicPoint = Ed448.validatePublicKeyPartialExport(buf, off);
111+
if (publicPoint == null)
112+
{
113+
throw new IllegalArgumentException("invalid public key");
114+
}
115+
return publicPoint;
116+
}
117+
91118
private static byte[] validate(byte[] buf)
92119
{
93120
if (buf.length != KEY_SIZE)

core/src/main/java/org/bouncycastle/math/ec/rfc7748/X25519Field.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,27 @@ public static void decode(int[] x, int xOff, int[] z)
152152
z[9] &= M24;
153153
}
154154

155+
public static void decode(byte[] x, int[] z)
156+
{
157+
decode128(x, 0, z, 0);
158+
decode128(x, 16, z, 5);
159+
z[9] &= M24;
160+
}
161+
155162
public static void decode(byte[] x, int xOff, int[] z)
156163
{
157164
decode128(x, xOff, z, 0);
158165
decode128(x, xOff + 16, z, 5);
159166
z[9] &= M24;
160167
}
161168

169+
public static void decode(byte[] x, int xOff, int[] z, int zOff)
170+
{
171+
decode128(x, xOff, z, zOff);
172+
decode128(x, xOff + 16, z, zOff + 5);
173+
z[zOff + 9] &= M24;
174+
}
175+
162176
private static void decode128(int[] is, int off, int[] z, int zOff)
163177
{
164178
int t0 = is[off + 0], t1 = is[off + 1], t2 = is[off + 2], t3 = is[off + 3];
@@ -199,12 +213,24 @@ public static void encode(int[] x, int[] z, int zOff)
199213
encode128(x, 5, z, zOff + 4);
200214
}
201215

216+
public static void encode(int[] x, byte[] z)
217+
{
218+
encode128(x, 0, z, 0);
219+
encode128(x, 5, z, 16);
220+
}
221+
202222
public static void encode(int[] x, byte[] z, int zOff)
203223
{
204224
encode128(x, 0, z, zOff);
205225
encode128(x, 5, z, zOff + 16);
206226
}
207227

228+
public static void encode(int[] x, int xOff, byte[] z, int zOff)
229+
{
230+
encode128(x, xOff, z, zOff);
231+
encode128(x, xOff + 5, z, zOff + 16);
232+
}
233+
208234
private static void encode128(int[] x, int xOff, int[] is, int off)
209235
{
210236
int x0 = x[xOff + 0], x1 = x[xOff + 1], x2 = x[xOff + 2], x3 = x[xOff + 3], x4 = x[xOff + 4];

0 commit comments

Comments
 (0)