Skip to content
Snippets Groups Projects
Commit 6a03752e authored by akwizgran's avatar akwizgran
Browse files

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

parent 623e7330
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment