Skip to content

Commit b74b6ec

Browse files
committed
More cleanups (thx zzz)
1 parent 5ad359f commit b74b6ec

File tree

7 files changed

+44
-26
lines changed

7 files changed

+44
-26
lines changed

src/net/i2p/crypto/eddsa/EdDSAEngine.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ protected byte[] engineSign() throws SignatureException {
137137
FieldElement S = curve.fromBigInteger(leEnc.decode(digest.digest(message)).multiply(a).add(rBI).mod(l));
138138

139139
// R+S
140-
ByteBuffer out = ByteBuffer.allocate(64);
140+
int b = curve.getField().getb();
141+
ByteBuffer out = ByteBuffer.allocate(b/4);
141142
out.put(Rbyte).put(S.toByteArray());
142143
return out.array();
143144
}
@@ -149,11 +150,8 @@ protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
149150
if (sigBytes.length != b/4)
150151
throw new SignatureException("signature length is wrong");
151152

152-
byte[] Rbyte = Arrays.copyOfRange(sigBytes, 0, b/8);
153-
byte[] Sbyte = Arrays.copyOfRange(sigBytes, b/8, b/4);
154-
155-
// If we get to here, Rbyte is valid
156-
digest.update(Rbyte);
153+
// R is first b/8 bytes of sigBytes, S is second b/8 bytes
154+
digest.update(sigBytes, 0, b/8);
157155
digest.update(((EdDSAPublicKey) key).getAbyte());
158156
// h = H(Rbar,Abar,M)
159157
byte[] message = baos.toByteArray();
@@ -163,14 +161,15 @@ protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
163161
LittleEndianEncoding leEnc = new LittleEndianEncoding();
164162
h = leEnc.encode(leEnc.decode(h).mod(key.getParams().getL()), b/8);
165163

164+
byte[] Sbyte = Arrays.copyOfRange(sigBytes, b/8, b/4);
166165
// R = SB - H(Rbar,Abar,M)A
167166
GroupElement R = key.getParams().getB().doubleScalarMultiplyVariableTime(
168167
((EdDSAPublicKey) key).getNegativeA(), h, Sbyte);
169168

170169
byte[] Rcalc = R.toByteArray();
171170
int result = 1;
172171
for (int i = 0; i < Rcalc.length; i++) {
173-
result &= Utils.equal(Rcalc[i], Rbyte[i]);
172+
result &= Utils.equal(Rcalc[i], sigBytes[i]);
174173
}
175174
return result == 1;
176175
}

src/net/i2p/crypto/eddsa/math/FieldElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public FieldElement multiply(FieldElement val) {
9292
}
9393

9494
public FieldElement square() {
95-
return modPow(BigInteger.valueOf(2), f.getQ());
95+
return modPow(Constants.TWO, f.getQ());
9696
}
9797

9898
public FieldElement squareAndDouble() {

src/net/i2p/crypto/eddsa/math/GroupElement.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,14 @@ public boolean equals(Object obj) {
405405
* Method is package private only so that tests run.
406406
*
407407
* @param a = a[0]+256*a[1]+...+256^31 a[31]
408-
* @return
408+
* @return 64 bytes, each between -8 and 7
409409
*/
410410
static byte[] toRadix16(byte[] a) {
411411
byte[] e = new byte[64];
412412
int i;
413413
// Radix 16 notation
414414
for (i = 0; i < 32; i++) {
415-
e[2*i+0] = (byte) ((a[i] >> 0) & 15);
415+
e[2*i+0] = (byte) (a[i] & 15);
416416
e[2*i+1] = (byte) ((a[i] >> 4) & 15);
417417
}
418418
/* each e[i] is between 0 and 15 */
@@ -422,10 +422,10 @@ static byte[] toRadix16(byte[] a) {
422422
e[i] += carry;
423423
carry = e[i] + 8;
424424
carry >>= 4;
425-
e[i] -= carry << 4;
425+
e[i] -= carry << 4;
426426
}
427427
e[63] += carry;
428-
/* each e[i] is between -8 and 8 */
428+
/* each e[i] is between -8 and 7 */
429429
return e;
430430
}
431431

@@ -442,12 +442,11 @@ static byte[] toRadix16(byte[] a) {
442442
*/
443443
GroupElement cmov(GroupElement u, int b) {
444444
GroupElement ret = null;
445-
int i;
446-
for (i = 0; i < b; i++) {
445+
for (int i = 0; i < b; i++) {
447446
// Only for b == 1
448447
ret = u;
449448
}
450-
for (i = 0; i < 1-b; i++) {
449+
for (int i = 0; i < 1-b; i++) {
451450
// Only for b == 0
452451
ret = this;
453452
}
@@ -457,6 +456,7 @@ GroupElement cmov(GroupElement u, int b) {
457456
/**
458457
* Look up 16^i r_i B in the precomputed table.
459458
* No secret array indices, no secret branching.
459+
* Constant time.
460460
*
461461
* Must have previously precomputed.
462462
*
@@ -492,6 +492,7 @@ GroupElement select(int pos, int b) {
492492
* h = a * Bb where a = a[0]+256*a[1]+...+256^31 a[31] and
493493
* B is this point. If its lookup table has not been precomputed, it
494494
* will be at the start of the method (and cached for later calls).
495+
* Constant time.
495496
*
496497
* Preconditions: (TODO: Check this applies here)
497498
* a[31] <= 127
@@ -531,28 +532,27 @@ public GroupElement scalarMultiply(byte[] a) {
531532
*
532533
* Method is package private only so that tests run.
533534
*
534-
* @param a
535-
* @return
535+
* @param a 32 bytes
536+
* @return 256 bytes
536537
*/
537538
static byte[] slide(byte[] a) {
538539
byte[] r = new byte[256];
539-
int i;
540-
int b;
541-
int k;
542540

543-
for (i = 0;i < 256;++i) {
541+
// put each bit of 'a' into a separate byte, 0 or 1
542+
for (int i = 0; i < 256; ++i) {
544543
r[i] = (byte) (1 & (a[i >> 3] >> (i & 7)));
545544
}
546545

547-
for (i = 0;i < 256;++i) {
546+
for (int i = 0; i < 256; ++i) {
548547
if (r[i] != 0) {
549-
for (b = 1; b <= 6 && i + b < 256; ++b) {
548+
for (int b = 1; b <= 6 && i + b < 256; ++b) {
550549
if (r[i + b] != 0) {
551550
if (r[i] + (r[i + b] << b) <= 15) {
552-
r[i] += r[i + b] << b; r[i + b] = 0;
551+
r[i] += r[i + b] << b;
552+
r[i + b] = 0;
553553
} else if (r[i] - (r[i + b] << b) >= -15) {
554554
r[i] -= r[i + b] << b;
555-
for (k = i + b; k < 256; ++k) {
555+
for (int k = i + b; k < 256; ++k) {
556556
if (r[k] == 0) {
557557
r[k] = 1;
558558
break;

src/net/i2p/crypto/eddsa/math/LittleEndianEncoding.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
public class LittleEndianEncoding implements Encoding, Serializable {
77
private static final long serialVersionUID = 3984579843759837L;
88

9+
/**
10+
* Convert x to little endian.
11+
* Constant time.
12+
*
13+
* @param len must be big enough
14+
* @return array of length len
15+
* @throws ArrayIndexOutOfBoundsException if len not big enough
16+
*/
917
public byte[] encode(BigInteger x, int len) {
1018
byte[] in = x.toByteArray();
1119
byte[] out = new byte[len];
@@ -18,8 +26,10 @@ public byte[] encode(BigInteger x, int len) {
1826
return out;
1927
}
2028

29+
/**
30+
* Convert in to big endian
31+
*/
2132
public BigInteger decode(byte[] in) {
22-
// Convert 'in' to big endian
2333
byte[] out = new byte[in.length];
2434
for (int i = 0; i < in.length; i++) {
2535
out[i] = in[in.length-1-i];

src/net/i2p/crypto/eddsa/spec/EdDSAParameterSpec.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class EdDSAParameterSpec implements AlgorithmParameterSpec, Serializable
2222
private final BigInteger l;
2323
private final GroupElement B;
2424

25+
/**
26+
* @throws IllegalArgumentException if hash algorithm is unsupported or length is wrong
27+
*/
2528
public EdDSAParameterSpec(Curve curve, String hashAlgo,
2629
BigInteger l, GroupElement B) {
2730
try {

src/net/i2p/crypto/eddsa/spec/EdDSAPrivateKeySpec.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public class EdDSAPrivateKeySpec implements KeySpec {
2020
private final GroupElement A;
2121
private final EdDSAParameterSpec spec;
2222

23+
/**
24+
* @throws IllegalArgumentException if hash algorithm is unsupported
25+
*/
2326
public EdDSAPrivateKeySpec(byte[] seed, EdDSAParameterSpec spec) {
2427
this.spec = spec;
2528
this.seed = seed;

src/net/i2p/crypto/eddsa/spec/EdDSAPublicKeySpec.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class EdDSAPublicKeySpec implements KeySpec {
1313
private final GroupElement Aneg;
1414
private final EdDSAParameterSpec spec;
1515

16+
/**
17+
* @throws IllegalArgumentException if key length is wrong
18+
*/
1619
public EdDSAPublicKeySpec(byte[] pk, EdDSAParameterSpec spec) {
1720
if (pk.length != spec.getCurve().getField().getb()/8)
1821
throw new IllegalArgumentException("public-key length is wrong");

0 commit comments

Comments
 (0)