diff options
author | David Hook <dgh@cryptoworkshop.com> | 2014-05-30 09:26:39 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2014-05-30 09:26:39 +0400 |
commit | 66bfe73d5d84b2a7bcf41d25010746eeb90d3b87 (patch) | |
tree | d9751e01ed6b20327a18a9d0ef668d14ee7407c2 /core/src/main/java | |
parent | 3c7ccc99e17f4ffdc5a31e90bedeb674b318d767 (diff) | |
parent | 7e84a85c46dfff2606ba9d6e603882d3605da051 (diff) |
Merge branch 'feature/scrypt-docs-params' of https://github.com/timw/bc-java into timw-feature/scrypt-docs-params
Diffstat (limited to 'core/src/main/java')
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/generators/SCrypt.java | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/generators/SCrypt.java b/core/src/main/java/org/bouncycastle/crypto/generators/SCrypt.java index 80cf3042..0b3dc147 100644 --- a/core/src/main/java/org/bouncycastle/crypto/generators/SCrypt.java +++ b/core/src/main/java/org/bouncycastle/crypto/generators/SCrypt.java @@ -7,11 +7,62 @@ import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.util.Arrays; import org.bouncycastle.util.Pack; +/** + * Implementation of the scrypt a password-based key derivation function. + * <p> + * Scrypt was created by Colin Percival and is specified in <a + * href="http://tools.ietf.org/html/draft-josefsson-scrypt-kdf-01">draft-josefsson-scrypt-kd</a> + * + */ public class SCrypt { - // TODO Validate arguments + /** + * Generate a key using the scrypt key derivation function. + * + * @param P the bytes of the pass phrase. + * @param S the salt to use for this invocation. + * @param N CPU/Memory cost parameter. Must be larger than 1, a power of 2 and less than + * <code>2^(128 * r / 8)</code>. + * @param r the block size, must be >= 1. + * @param p Parallelization parameter. Must be a positive integer less than or equal to + * <code>Integer.MAX_VALUE / (128 * r * 8)</code>. + * + * @param dkLen the length of the key to generate. + * @return the generated key. + */ public static byte[] generate(byte[] P, byte[] S, int N, int r, int p, int dkLen) { + if (P== null) + { + throw new IllegalArgumentException("Passphrase P must be provided."); + } + if (S == null) + { + throw new IllegalArgumentException("Salt S must be provided."); + } + if (N <= 1) + { + throw new IllegalArgumentException("Cost parameter N must be > 1."); + } + // Only value of r that cost (as an int) could be exceeded for is 1 + if (r == 1 && N > 65536) + { + throw new IllegalArgumentException("Cost parameter N must be > 1 and < 65536."); + } + if (r < 1) + { + throw new IllegalArgumentException("Block size r must be >= 1."); + } + int maxParallel = Integer.MAX_VALUE / (128 * r * 8); + if (p < 1 || p > maxParallel) + { + throw new IllegalArgumentException("Parallelisation parameter p must be >= 1 and <= " + maxParallel + + " (based on block size r of " + r + ")"); + } + if (dkLen < 1) + { + throw new IllegalArgumentException("Generated key length dkLen must be >= 1."); + } return MFcrypt(P, S, N, r, p, dkLen); } |