diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2018-04-17 14:26:27 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2018-04-17 14:26:27 +0700 |
commit | 87d10a3c94287804e8df25be360b0e612dc343d7 (patch) | |
tree | 69055968d7944f879098d42dc58ee389f6d973e8 /crypto/src | |
parent | Update TSP digest tables from bc-java (diff) | |
download | BouncyCastle.NET-ed25519-87d10a3c94287804e8df25be360b0e612dc343d7.tar.xz |
SCrypt: N parameters must be a power of 2
Diffstat (limited to 'crypto/src')
-rw-r--r-- | crypto/src/crypto/generators/SCrypt.cs | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/crypto/src/crypto/generators/SCrypt.cs b/crypto/src/crypto/generators/SCrypt.cs index 64a36df63..4d15bb3d7 100644 --- a/crypto/src/crypto/generators/SCrypt.cs +++ b/crypto/src/crypto/generators/SCrypt.cs @@ -1,5 +1,5 @@ using System; -using System.Threading; +using System.Diagnostics; using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Engines; @@ -31,8 +31,8 @@ namespace Org.BouncyCastle.Crypto.Generators throw new ArgumentNullException("Passphrase P must be provided."); if (S == null) throw new ArgumentNullException("Salt S must be provided."); - if (N <= 1) - throw new ArgumentException("Cost parameter N must be > 1."); + if (N <= 1 || !IsPowerOf2(N)) + throw new ArgumentException("Cost parameter N must be > 1 and a power of 2."); // Only value of r that cost (as an int) could be exceeded for is 1 if (r == 1 && N >= 65536) throw new ArgumentException("Cost parameter N must be > 1 and < 65536."); @@ -170,5 +170,13 @@ namespace Org.BouncyCastle.Crypto.Generators Clear(array); } } - } + + // note: we know X is non-zero + private static bool IsPowerOf2(int x) + { + Debug.Assert(x != 0); + + return (x & (x - 1)) == 0; + } + } } |