mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
When integers are converted to fixed length, ensure any padding is zero.
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user