From 9ded2c195eb68ed349f86f781bf30e723846cbf2 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Mon, 30 Mar 2015 20:34:13 +0300 Subject: [PATCH] Improved modPow for 255-byte results Possibly related to #749 --- app/js/lib/bin_utils.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/app/js/lib/bin_utils.js b/app/js/lib/bin_utils.js index 2ffd3791..d007cf18 100644 --- a/app/js/lib/bin_utils.js +++ b/app/js/lib/bin_utils.js @@ -191,8 +191,21 @@ function bytesFromWords (wordArray) { function bytesFromBigInt (bigInt, len) { var bytes = bigInt.toByteArray(); - while (!bytes[0] && (!len || bytes.length > len)) { - bytes = bytes.slice(1); + if (len && bytes.length < len) { + var padding = []; + for (var i = 0, needPadding = len - bytes.length; i < needPadding; i++) { + padding[i] = 0; + } + if (bytes instanceof ArrayBuffer) { + bytes = bufferConcat(padding, bytes); + } else { + bytes = padding.concat(bytes); + } + } + else { + while (!bytes[0] && (!len || bytes.length > len)) { + bytes = bytes.slice(1); + } } return bytes; @@ -320,13 +333,19 @@ function rsaEncrypt (publicKey, bytes) { return encryptedBytes; } -function addPadding(bytes, blockSize) { +function addPadding(bytes, blockSize, zeroes) { blockSize = blockSize || 16; var len = bytes.byteLength || bytes.length; var needPadding = blockSize - (len % blockSize); if (needPadding > 0 && needPadding < blockSize) { var padding = new Array(needPadding); - (new SecureRandom()).nextBytes(padding); + if (zeroes) { + for (var i = 0; i < needPadding; i++) { + padding[i] = 0 + } + } else { + (new SecureRandom()).nextBytes(padding); + } if (bytes instanceof ArrayBuffer) { bytes = bufferConcat(bytes, padding); @@ -383,7 +402,7 @@ function nextRandomInt (maxValue) { }; function pqPrimeFactorization (pqBytes) { - var what = new BigInteger(pqBytes), + var what = new BigInteger(pqBytes), result = false; // console.log(dT(), 'PQ start', pqBytes, what.toString(16), what.bitLength()); @@ -635,5 +654,5 @@ function bytesModPow (x, y, m) { console.error('mod pow error', e); } - return bytesFromBigInt(new BigInteger(x).modPow(new BigInteger(y), new BigInteger(m))); + return bytesFromBigInt(new BigInteger(x).modPow(new BigInteger(y), new BigInteger(m)), 256); }