From 6a03752e4b2bf360383be42c36d07ddc4ad94ef2 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Fri, 10 Jan 2014 15:13:09 +0000
Subject: [PATCH] When integers are converted to fixed length, ensure any
 padding is zero.

---
 .../src/org/briarproject/crypto/Sec1PrivateKey.java      | 2 +-
 .../src/org/briarproject/crypto/Sec1PublicKey.java       | 6 +++---
 briar-core/src/org/briarproject/crypto/Sec1Utils.java    | 9 +++++----
 3 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/briar-core/src/org/briarproject/crypto/Sec1PrivateKey.java b/briar-core/src/org/briarproject/crypto/Sec1PrivateKey.java
index 2cf5ab782e..256001dbaf 100644
--- a/briar-core/src/org/briarproject/crypto/Sec1PrivateKey.java
+++ b/briar-core/src/org/briarproject/crypto/Sec1PrivateKey.java
@@ -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;
 	}
 
diff --git a/briar-core/src/org/briarproject/crypto/Sec1PublicKey.java b/briar-core/src/org/briarproject/crypto/Sec1PublicKey.java
index 0b749384af..eefdfde74b 100644
--- a/briar-core/src/org/briarproject/crypto/Sec1PublicKey.java
+++ b/briar-core/src/org/briarproject/crypto/Sec1PublicKey.java
@@ -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;
 	}
 
diff --git a/briar-core/src/org/briarproject/crypto/Sec1Utils.java b/briar-core/src/org/briarproject/crypto/Sec1Utils.java
index b0f14ca1ba..23e1f86526 100644
--- a/briar-core/src/org/briarproject/crypto/Sec1Utils.java
+++ b/briar-core/src/org/briarproject/crypto/Sec1Utils.java
@@ -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);
-- 
GitLab