When integers are converted to fixed length, ensure any padding is zero.

This commit is contained in:
akwizgran
2014-01-10 15:13:09 +00:00
parent 623e7330ed
commit 6a03752e4b
3 changed files with 9 additions and 8 deletions

View File

@@ -17,7 +17,7 @@ class Sec1PrivateKey implements PrivateKey {
public byte[] getEncoded() {
byte[] encodedKey = new byte[bytesPerInt];
byte[] d = key.getD().toByteArray();
Sec1Utils.convertToFixedLength(d, encodedKey, bytesPerInt, 0);
Sec1Utils.convertToFixedLength(d, encodedKey, 0, bytesPerInt);
return encodedKey;
}

View File

@@ -24,10 +24,10 @@ class Sec1PublicKey implements PublicKey {
byte[] encodedKey = new byte[publicKeyBytes];
encodedKey[0] = 4;
byte[] x = key.getQ().getX().toBigInteger().toByteArray();
Sec1Utils.convertToFixedLength(x, encodedKey, bytesPerInt, 1);
Sec1Utils.convertToFixedLength(x, encodedKey, 1, bytesPerInt);
byte[] y = key.getQ().getY().toBigInteger().toByteArray();
Sec1Utils.convertToFixedLength(y, encodedKey, bytesPerInt,
1 + bytesPerInt);
Sec1Utils.convertToFixedLength(y, encodedKey, 1 + bytesPerInt,
bytesPerInt);
return encodedKey;
}

View File

@@ -2,11 +2,12 @@ package org.briarproject.crypto;
class Sec1Utils {
static void convertToFixedLength(byte[] src, byte[] dest, int destLen,
int destOff) {
static void convertToFixedLength(byte[] src, byte[] dest, int destOff,
int destLen) {
if(src.length < destLen) {
destOff += destLen - src.length;
System.arraycopy(src, 0, dest, destOff, src.length);
int padding = destLen - src.length;
for(int i = destOff; i < destOff + padding; i++) dest[i] = 0;
System.arraycopy(src, 0, dest, destOff + padding, src.length);
} else {
int srcOff = src.length - destLen;
System.arraycopy(src, srcOff, dest, destOff, destLen);