|
6 | 6 | import org.bouncycastle.crypto.util.RadixConverter; |
7 | 7 | import org.bouncycastle.util.Arrays; |
8 | 8 | import org.bouncycastle.util.BigIntegers; |
| 9 | +import org.bouncycastle.util.Integers; |
9 | 10 | import org.bouncycastle.util.Pack; |
10 | 11 |
|
11 | 12 | /* |
@@ -64,8 +65,8 @@ static short[] decFF1(BlockCipher cipher, RadixConverter radixConverter, byte[] |
64 | 65 | { |
65 | 66 | int radix = radixConverter.getRadix(); |
66 | 67 | int t = T.length; |
67 | | - int b = ((int)Math.ceil(Math.log((double)radix) * (double)v / LOG2) + 7) / 8; |
68 | | - int d = (((b + 3) / 4) * 4) + 4; |
| 68 | + int b = calculateB_FF1(radix, v); |
| 69 | + int d = (b + 7) & ~3; |
69 | 70 |
|
70 | 71 | byte[] P = calculateP_FF1(radix, (byte)u, n, t); |
71 | 72 |
|
@@ -172,8 +173,8 @@ private static short[] encFF1(BlockCipher cipher, RadixConverter radixConverter, |
172 | 173 | int radix = radixConverter.getRadix(); |
173 | 174 | int t = T.length; |
174 | 175 |
|
175 | | - int b = ((int)Math.ceil(Math.log((double)radix) * (double)v / LOG2) + 7) / 8; |
176 | | - int d = (((b + 3) / 4) * 4) + 4; |
| 176 | + int b = calculateB_FF1(radix, v); |
| 177 | + int d = (b + 7) & ~3; |
177 | 178 |
|
178 | 179 | byte[] P = calculateP_FF1(radix, (byte)u, n, t); |
179 | 180 |
|
@@ -256,6 +257,26 @@ static byte[] encryptFF3_1(BlockCipher cipher, RadixConverter radixConverter, by |
256 | 257 | return encryptFF3(cipher, radixConverter, tweak64, buf, off, len); |
257 | 258 | } |
258 | 259 |
|
| 260 | + protected static int calculateB_FF1(int radix, int v) |
| 261 | + { |
| 262 | +// return (BigInteger.valueOf(radix).pow(v).subtract(BigInteger.ONE).bitLength() + 7) / 8; |
| 263 | + |
| 264 | + int powersOfTwo = Integers.numberOfTrailingZeros(radix); |
| 265 | + int bits = powersOfTwo * v; |
| 266 | + |
| 267 | + int oddPart = radix >>> powersOfTwo; |
| 268 | + if (oddPart != 1) |
| 269 | + { |
| 270 | + // Old version with rounding issues, especially for power of 2 radix, but maybe others. |
| 271 | +// bits += (int)Math.ceil(Math.log((double)oddPart) * (double)v / LOG2); |
| 272 | + |
| 273 | + // Exact calculation, with possible performance issues if v is too large. |
| 274 | + bits += BigInteger.valueOf(oddPart).pow(v).bitLength(); |
| 275 | + } |
| 276 | + |
| 277 | + return (bits + 7) / 8; |
| 278 | + } |
| 279 | + |
259 | 280 | protected static BigInteger[] calculateModUV(BigInteger bigRadix, int u, int v) |
260 | 281 | { |
261 | 282 | BigInteger[] modUV = new BigInteger[2]; |
|
0 commit comments