diff options
Diffstat (limited to 'core/src/main/java/org/bouncycastle/crypto/params')
56 files changed, 2609 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/params/AEADParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/AEADParameters.java new file mode 100644 index 00000000..9a9272ba --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/AEADParameters.java @@ -0,0 +1,60 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +public class AEADParameters + implements CipherParameters +{ + private byte[] associatedText; + private byte[] nonce; + private KeyParameter key; + private int macSize; + + /** + * Base constructor. + * + * @param key key to be used by underlying cipher + * @param macSize macSize in bits + * @param nonce nonce to be used + */ + public AEADParameters(KeyParameter key, int macSize, byte[] nonce) + { + this(key, macSize, nonce, null); + } + + /** + * Base constructor. + * + * @param key key to be used by underlying cipher + * @param macSize macSize in bits + * @param nonce nonce to be used + * @param associatedText initial associated text, if any + */ + public AEADParameters(KeyParameter key, int macSize, byte[] nonce, byte[] associatedText) + { + this.key = key; + this.nonce = nonce; + this.macSize = macSize; + this.associatedText = associatedText; + } + + public KeyParameter getKey() + { + return key; + } + + public int getMacSize() + { + return macSize; + } + + public byte[] getAssociatedText() + { + return associatedText; + } + + public byte[] getNonce() + { + return nonce; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/AsymmetricKeyParameter.java b/core/src/main/java/org/bouncycastle/crypto/params/AsymmetricKeyParameter.java new file mode 100644 index 00000000..03ba7253 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/AsymmetricKeyParameter.java @@ -0,0 +1,20 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +public class AsymmetricKeyParameter + implements CipherParameters +{ + boolean privateKey; + + public AsymmetricKeyParameter( + boolean privateKey) + { + this.privateKey = privateKey; + } + + public boolean isPrivate() + { + return privateKey; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/CCMParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/CCMParameters.java new file mode 100644 index 00000000..4924dcca --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/CCMParameters.java @@ -0,0 +1,21 @@ +package org.bouncycastle.crypto.params; + +/** + * @deprecated use AEADParameters + */ +public class CCMParameters + extends AEADParameters +{ + /** + * Base constructor. + * + * @param key key to be used by underlying cipher + * @param macSize macSize in bits + * @param nonce nonce to be used + * @param associatedText associated text, if any + */ + public CCMParameters(KeyParameter key, int macSize, byte[] nonce, byte[] associatedText) + { + super(key, macSize, nonce, associatedText); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DESParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DESParameters.java new file mode 100644 index 00000000..5bee3604 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DESParameters.java @@ -0,0 +1,107 @@ +package org.bouncycastle.crypto.params; + +public class DESParameters + extends KeyParameter +{ + public DESParameters( + byte[] key) + { + super(key); + + if (isWeakKey(key, 0)) + { + throw new IllegalArgumentException("attempt to create weak DES key"); + } + } + + /* + * DES Key length in bytes. + */ + static public final int DES_KEY_LENGTH = 8; + + /* + * Table of weak and semi-weak keys taken from Schneier pp281 + */ + static private final int N_DES_WEAK_KEYS = 16; + + static private byte[] DES_weak_keys = + { + /* weak keys */ + (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01, (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01, + (byte)0x1f,(byte)0x1f,(byte)0x1f,(byte)0x1f, (byte)0x0e,(byte)0x0e,(byte)0x0e,(byte)0x0e, + (byte)0xe0,(byte)0xe0,(byte)0xe0,(byte)0xe0, (byte)0xf1,(byte)0xf1,(byte)0xf1,(byte)0xf1, + (byte)0xfe,(byte)0xfe,(byte)0xfe,(byte)0xfe, (byte)0xfe,(byte)0xfe,(byte)0xfe,(byte)0xfe, + + /* semi-weak keys */ + (byte)0x01,(byte)0xfe,(byte)0x01,(byte)0xfe, (byte)0x01,(byte)0xfe,(byte)0x01,(byte)0xfe, + (byte)0x1f,(byte)0xe0,(byte)0x1f,(byte)0xe0, (byte)0x0e,(byte)0xf1,(byte)0x0e,(byte)0xf1, + (byte)0x01,(byte)0xe0,(byte)0x01,(byte)0xe0, (byte)0x01,(byte)0xf1,(byte)0x01,(byte)0xf1, + (byte)0x1f,(byte)0xfe,(byte)0x1f,(byte)0xfe, (byte)0x0e,(byte)0xfe,(byte)0x0e,(byte)0xfe, + (byte)0x01,(byte)0x1f,(byte)0x01,(byte)0x1f, (byte)0x01,(byte)0x0e,(byte)0x01,(byte)0x0e, + (byte)0xe0,(byte)0xfe,(byte)0xe0,(byte)0xfe, (byte)0xf1,(byte)0xfe,(byte)0xf1,(byte)0xfe, + (byte)0xfe,(byte)0x01,(byte)0xfe,(byte)0x01, (byte)0xfe,(byte)0x01,(byte)0xfe,(byte)0x01, + (byte)0xe0,(byte)0x1f,(byte)0xe0,(byte)0x1f, (byte)0xf1,(byte)0x0e,(byte)0xf1,(byte)0x0e, + (byte)0xe0,(byte)0x01,(byte)0xe0,(byte)0x01, (byte)0xf1,(byte)0x01,(byte)0xf1,(byte)0x01, + (byte)0xfe,(byte)0x1f,(byte)0xfe,(byte)0x1f, (byte)0xfe,(byte)0x0e,(byte)0xfe,(byte)0x0e, + (byte)0x1f,(byte)0x01,(byte)0x1f,(byte)0x01, (byte)0x0e,(byte)0x01,(byte)0x0e,(byte)0x01, + (byte)0xfe,(byte)0xe0,(byte)0xfe,(byte)0xe0, (byte)0xfe,(byte)0xf1,(byte)0xfe,(byte)0xf1 + }; + + /** + * DES has 16 weak keys. This method will check + * if the given DES key material is weak or semi-weak. + * Key material that is too short is regarded as weak. + * <p> + * See <a href="http://www.counterpane.com/applied.html">"Applied + * Cryptography"</a> by Bruce Schneier for more information. + * + * @return true if the given DES key material is weak or semi-weak, + * false otherwise. + */ + public static boolean isWeakKey( + byte[] key, + int offset) + { + if (key.length - offset < DES_KEY_LENGTH) + { + throw new IllegalArgumentException("key material too short."); + } + + nextkey: for (int i = 0; i < N_DES_WEAK_KEYS; i++) + { + for (int j = 0; j < DES_KEY_LENGTH; j++) + { + if (key[j + offset] != DES_weak_keys[i * DES_KEY_LENGTH + j]) + { + continue nextkey; + } + } + + return true; + } + return false; + } + + /** + * DES Keys use the LSB as the odd parity bit. This can + * be used to check for corrupt keys. + * + * @param bytes the byte array to set the parity on. + */ + public static void setOddParity( + byte[] bytes) + { + for (int i = 0; i < bytes.length; i++) + { + int b = bytes[i]; + bytes[i] = (byte)((b & 0xfe) | + ((((b >> 1) ^ + (b >> 2) ^ + (b >> 3) ^ + (b >> 4) ^ + (b >> 5) ^ + (b >> 6) ^ + (b >> 7)) ^ 0x01) & 0x01)); + } + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DESedeParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DESedeParameters.java new file mode 100644 index 00000000..3a4bbfca --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DESedeParameters.java @@ -0,0 +1,57 @@ +package org.bouncycastle.crypto.params; + +public class DESedeParameters + extends DESParameters +{ + /* + * DES-EDE Key length in bytes. + */ + static public final int DES_EDE_KEY_LENGTH = 24; + + public DESedeParameters( + byte[] key) + { + super(key); + + if (isWeakKey(key, 0, key.length)) + { + throw new IllegalArgumentException("attempt to create weak DESede key"); + } + } + + /** + * return true if the passed in key is a DES-EDE weak key. + * + * @param key bytes making up the key + * @param offset offset into the byte array the key starts at + * @param length number of bytes making up the key + */ + public static boolean isWeakKey( + byte[] key, + int offset, + int length) + { + for (int i = offset; i < length; i += DES_KEY_LENGTH) + { + if (DESParameters.isWeakKey(key, i)) + { + return true; + } + } + + return false; + } + + /** + * return true if the passed in key is a DES-EDE weak key. + * + * @param key bytes making up the key + * @param offset offset into the byte array the key starts at + */ + public static boolean isWeakKey( + byte[] key, + int offset) + { + return isWeakKey(key, offset, key.length - offset); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DHKeyGenerationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DHKeyGenerationParameters.java new file mode 100644 index 00000000..34c730eb --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DHKeyGenerationParameters.java @@ -0,0 +1,30 @@ +package org.bouncycastle.crypto.params; + +import java.security.SecureRandom; + +import org.bouncycastle.crypto.KeyGenerationParameters; + +public class DHKeyGenerationParameters + extends KeyGenerationParameters +{ + private DHParameters params; + + public DHKeyGenerationParameters( + SecureRandom random, + DHParameters params) + { + super(random, getStrength(params)); + + this.params = params; + } + + public DHParameters getParameters() + { + return params; + } + + static int getStrength(DHParameters params) + { + return params.getL() != 0 ? params.getL() : params.getP().bitLength(); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DHKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DHKeyParameters.java new file mode 100644 index 00000000..e686f357 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DHKeyParameters.java @@ -0,0 +1,54 @@ +package org.bouncycastle.crypto.params; + + +public class DHKeyParameters + extends AsymmetricKeyParameter +{ + private DHParameters params; + + protected DHKeyParameters( + boolean isPrivate, + DHParameters params) + { + super(isPrivate); + + this.params = params; + } + + public DHParameters getParameters() + { + return params; + } + + public boolean equals( + Object obj) + { + if (!(obj instanceof DHKeyParameters)) + { + return false; + } + + DHKeyParameters dhKey = (DHKeyParameters)obj; + + if (params == null) + { + return dhKey.getParameters() == null; + } + else + { + return params.equals(dhKey.getParameters()); + } + } + + public int hashCode() + { + int code = isPrivate() ? 0 : 1; + + if (params != null) + { + code ^= params.hashCode(); + } + + return code; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DHParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DHParameters.java new file mode 100644 index 00000000..b679287c --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DHParameters.java @@ -0,0 +1,189 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +import org.bouncycastle.crypto.CipherParameters; + +public class DHParameters + implements CipherParameters +{ + private static final int DEFAULT_MINIMUM_LENGTH = 160; + + // not final due to compiler bug in "simpler" JDKs + private BigInteger g; + private BigInteger p; + private BigInteger q; + private BigInteger j; + private int m; + private int l; + private DHValidationParameters validation; + + private static int getDefaultMParam( + int lParam) + { + if (lParam == 0) + { + return DEFAULT_MINIMUM_LENGTH; + } + + return lParam < DEFAULT_MINIMUM_LENGTH ? lParam : DEFAULT_MINIMUM_LENGTH; + } + + public DHParameters( + BigInteger p, + BigInteger g) + { + this(p, g, null, 0); + } + + public DHParameters( + BigInteger p, + BigInteger g, + BigInteger q) + { + this(p, g, q, 0); + } + + public DHParameters( + BigInteger p, + BigInteger g, + BigInteger q, + int l) + { + this(p, g, q, getDefaultMParam(l), l, null, null); + } + + public DHParameters( + BigInteger p, + BigInteger g, + BigInteger q, + int m, + int l) + { + this(p, g, q, m, l, null, null); + } + + public DHParameters( + BigInteger p, + BigInteger g, + BigInteger q, + BigInteger j, + DHValidationParameters validation) + { + this(p, g, q, DEFAULT_MINIMUM_LENGTH, 0, j, validation); + } + + public DHParameters( + BigInteger p, + BigInteger g, + BigInteger q, + int m, + int l, + BigInteger j, + DHValidationParameters validation) + { + if (l != 0) + { + BigInteger bigL = BigInteger.valueOf(2L ^ (l - 1)); + if (bigL.compareTo(p) == 1) + { + throw new IllegalArgumentException("when l value specified, it must satisfy 2^(l-1) <= p"); + } + if (l < m) + { + throw new IllegalArgumentException("when l value specified, it may not be less than m value"); + } + } + + this.g = g; + this.p = p; + this.q = q; + this.m = m; + this.l = l; + this.j = j; + this.validation = validation; + } + + public BigInteger getP() + { + return p; + } + + public BigInteger getG() + { + return g; + } + + public BigInteger getQ() + { + return q; + } + + /** + * Return the subgroup factor J. + * + * @return subgroup factor + */ + public BigInteger getJ() + { + return j; + } + + /** + * Return the minimum length of the private value. + * + * @return the minimum length of the private value in bits. + */ + public int getM() + { + return m; + } + + /** + * Return the private value length in bits - if set, zero otherwise + * + * @return the private value length in bits, zero otherwise. + */ + public int getL() + { + return l; + } + + public DHValidationParameters getValidationParameters() + { + return validation; + } + + public boolean equals( + Object obj) + { + if (!(obj instanceof DHParameters)) + { + return false; + } + + DHParameters pm = (DHParameters)obj; + + if (this.getQ() != null) + { + if (!this.getQ().equals(pm.getQ())) + { + return false; + } + } + else + { + if (pm.getQ() != null) + { + return false; + } + } + + return pm.getP().equals(p) && pm.getG().equals(g); + } + + public int hashCode() + { + return getP().hashCode() ^ getG().hashCode() ^ (getQ() != null ? getQ().hashCode() : 0); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DHPrivateKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DHPrivateKeyParameters.java new file mode 100644 index 00000000..ee1b34f9 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DHPrivateKeyParameters.java @@ -0,0 +1,41 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class DHPrivateKeyParameters + extends DHKeyParameters +{ + private BigInteger x; + + public DHPrivateKeyParameters( + BigInteger x, + DHParameters params) + { + super(true, params); + + this.x = x; + } + + public BigInteger getX() + { + return x; + } + + public int hashCode() + { + return x.hashCode() ^ super.hashCode(); + } + + public boolean equals( + Object obj) + { + if (!(obj instanceof DHPrivateKeyParameters)) + { + return false; + } + + DHPrivateKeyParameters other = (DHPrivateKeyParameters)obj; + + return other.getX().equals(this.x) && super.equals(obj); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DHPublicKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DHPublicKeyParameters.java new file mode 100644 index 00000000..ed531603 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DHPublicKeyParameters.java @@ -0,0 +1,41 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class DHPublicKeyParameters + extends DHKeyParameters +{ + private BigInteger y; + + public DHPublicKeyParameters( + BigInteger y, + DHParameters params) + { + super(false, params); + + this.y = y; + } + + public BigInteger getY() + { + return y; + } + + public int hashCode() + { + return y.hashCode() ^ super.hashCode(); + } + + public boolean equals( + Object obj) + { + if (!(obj instanceof DHPublicKeyParameters)) + { + return false; + } + + DHPublicKeyParameters other = (DHPublicKeyParameters)obj; + + return other.getY().equals(y) && super.equals(obj); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DHValidationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DHValidationParameters.java new file mode 100644 index 00000000..b22f7a03 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DHValidationParameters.java @@ -0,0 +1,50 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.util.Arrays; + +public class DHValidationParameters +{ + private byte[] seed; + private int counter; + + public DHValidationParameters( + byte[] seed, + int counter) + { + this.seed = seed; + this.counter = counter; + } + + public int getCounter() + { + return counter; + } + + public byte[] getSeed() + { + return seed; + } + + public boolean equals( + Object o) + { + if (!(o instanceof DHValidationParameters)) + { + return false; + } + + DHValidationParameters other = (DHValidationParameters)o; + + if (other.counter != this.counter) + { + return false; + } + + return Arrays.areEqual(this.seed, other.seed); + } + + public int hashCode() + { + return counter ^ Arrays.hashCode(seed); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DSAKeyGenerationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DSAKeyGenerationParameters.java new file mode 100644 index 00000000..29fa91e6 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DSAKeyGenerationParameters.java @@ -0,0 +1,25 @@ +package org.bouncycastle.crypto.params; + +import java.security.SecureRandom; + +import org.bouncycastle.crypto.KeyGenerationParameters; + +public class DSAKeyGenerationParameters + extends KeyGenerationParameters +{ + private DSAParameters params; + + public DSAKeyGenerationParameters( + SecureRandom random, + DSAParameters params) + { + super(random, params.getP().bitLength() - 1); + + this.params = params; + } + + public DSAParameters getParameters() + { + return params; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DSAKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DSAKeyParameters.java new file mode 100644 index 00000000..11bb9d97 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DSAKeyParameters.java @@ -0,0 +1,21 @@ +package org.bouncycastle.crypto.params; + +public class DSAKeyParameters + extends AsymmetricKeyParameter +{ + private DSAParameters params; + + public DSAKeyParameters( + boolean isPrivate, + DSAParameters params) + { + super(isPrivate); + + this.params = params; + } + + public DSAParameters getParameters() + { + return params; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DSAParameterGenerationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DSAParameterGenerationParameters.java new file mode 100644 index 00000000..ba841b8e --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DSAParameterGenerationParameters.java @@ -0,0 +1,80 @@ +package org.bouncycastle.crypto.params; + +import java.security.SecureRandom; + +public class DSAParameterGenerationParameters +{ + public static final int DIGITAL_SIGNATURE_USAGE = 1; + public static final int KEY_ESTABLISHMENT_USAGE = 2; + + private final int l; + private final int n; + private final int usageIndex; + private final int certainty; + private final SecureRandom random; + + /** + * Construct without a usage index, this will do a random construction of G. + * + * @param L desired length of prime P in bits (the effective key size). + * @param N desired length of prime Q in bits. + * @param certainty certainty level for prime number generation. + * @param random the source of randomness to use. + */ + public DSAParameterGenerationParameters( + int L, + int N, + int certainty, + SecureRandom random) + { + this(L, N, certainty, random, -1); + } + + /** + * Construct for a specific usage index - this has the effect of using verifiable canonical generation of G. + * + * @param L desired length of prime P in bits (the effective key size). + * @param N desired length of prime Q in bits. + * @param certainty certainty level for prime number generation. + * @param random the source of randomness to use. + * @param usageIndex a valid usage index. + */ + public DSAParameterGenerationParameters( + int L, + int N, + int certainty, + SecureRandom random, + int usageIndex) + { + this.l = L; + this.n = N; + this.certainty = certainty; + this.usageIndex = usageIndex; + this.random = random; + } + + public int getL() + { + return l; + } + + public int getN() + { + return n; + } + + public int getCertainty() + { + return certainty; + } + + public SecureRandom getRandom() + { + return random; + } + + public int getUsageIndex() + { + return usageIndex; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DSAParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DSAParameters.java new file mode 100644 index 00000000..7f76d117 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DSAParameters.java @@ -0,0 +1,74 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +import org.bouncycastle.crypto.CipherParameters; + +public class DSAParameters + implements CipherParameters +{ + private BigInteger g; + private BigInteger q; + private BigInteger p; + private DSAValidationParameters validation; + + public DSAParameters( + BigInteger p, + BigInteger q, + BigInteger g) + { + this.g = g; + this.p = p; + this.q = q; + } + + public DSAParameters( + BigInteger p, + BigInteger q, + BigInteger g, + DSAValidationParameters params) + { + this.g = g; + this.p = p; + this.q = q; + this.validation = params; + } + + public BigInteger getP() + { + return p; + } + + public BigInteger getQ() + { + return q; + } + + public BigInteger getG() + { + return g; + } + + public DSAValidationParameters getValidationParameters() + { + return validation; + } + + public boolean equals( + Object obj) + { + if (!(obj instanceof DSAParameters)) + { + return false; + } + + DSAParameters pm = (DSAParameters)obj; + + return (pm.getP().equals(p) && pm.getQ().equals(q) && pm.getG().equals(g)); + } + + public int hashCode() + { + return getP().hashCode() ^ getQ().hashCode() ^ getG().hashCode(); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DSAPrivateKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DSAPrivateKeyParameters.java new file mode 100644 index 00000000..3bef3f40 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DSAPrivateKeyParameters.java @@ -0,0 +1,23 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class DSAPrivateKeyParameters + extends DSAKeyParameters +{ + private BigInteger x; + + public DSAPrivateKeyParameters( + BigInteger x, + DSAParameters params) + { + super(true, params); + + this.x = x; + } + + public BigInteger getX() + { + return x; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DSAPublicKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DSAPublicKeyParameters.java new file mode 100644 index 00000000..c0066568 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DSAPublicKeyParameters.java @@ -0,0 +1,23 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class DSAPublicKeyParameters + extends DSAKeyParameters +{ + private BigInteger y; + + public DSAPublicKeyParameters( + BigInteger y, + DSAParameters params) + { + super(false, params); + + this.y = y; + } + + public BigInteger getY() + { + return y; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/DSAValidationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/DSAValidationParameters.java new file mode 100644 index 00000000..07d93d07 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/DSAValidationParameters.java @@ -0,0 +1,65 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.util.Arrays; + +public class DSAValidationParameters +{ + private int usageIndex; + private byte[] seed; + private int counter; + + public DSAValidationParameters( + byte[] seed, + int counter) + { + this(seed, counter, -1); + } + + public DSAValidationParameters( + byte[] seed, + int counter, + int usageIndex) + { + this.seed = seed; + this.counter = counter; + this.usageIndex = usageIndex; + } + + public int getCounter() + { + return counter; + } + + public byte[] getSeed() + { + return seed; + } + + public int getUsageIndex() + { + return usageIndex; + } + + public int hashCode() + { + return counter ^ Arrays.hashCode(seed); + } + + public boolean equals( + Object o) + { + if (!(o instanceof DSAValidationParameters)) + { + return false; + } + + DSAValidationParameters other = (DSAValidationParameters)o; + + if (other.counter != this.counter) + { + return false; + } + + return Arrays.areEqual(this.seed, other.seed); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ECDomainParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ECDomainParameters.java new file mode 100644 index 00000000..05a13274 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ECDomainParameters.java @@ -0,0 +1,74 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +import org.bouncycastle.math.ec.ECConstants; +import org.bouncycastle.math.ec.ECCurve; +import org.bouncycastle.math.ec.ECPoint; +import org.bouncycastle.util.Arrays; + +public class ECDomainParameters + implements ECConstants +{ + private ECCurve curve; + private byte[] seed; + private ECPoint G; + private BigInteger n; + private BigInteger h; + + public ECDomainParameters( + ECCurve curve, + ECPoint G, + BigInteger n) + { + this(curve, G, n, ONE, null); + } + + public ECDomainParameters( + ECCurve curve, + ECPoint G, + BigInteger n, + BigInteger h) + { + this(curve, G, n, h, null); + } + + public ECDomainParameters( + ECCurve curve, + ECPoint G, + BigInteger n, + BigInteger h, + byte[] seed) + { + this.curve = curve; + this.G = G; + this.n = n; + this.h = h; + this.seed = seed; + } + + public ECCurve getCurve() + { + return curve; + } + + public ECPoint getG() + { + return G; + } + + public BigInteger getN() + { + return n; + } + + public BigInteger getH() + { + return h; + } + + public byte[] getSeed() + { + return Arrays.clone(seed); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ECKeyGenerationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ECKeyGenerationParameters.java new file mode 100644 index 00000000..be3f20f5 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ECKeyGenerationParameters.java @@ -0,0 +1,25 @@ +package org.bouncycastle.crypto.params; + +import java.security.SecureRandom; + +import org.bouncycastle.crypto.KeyGenerationParameters; + +public class ECKeyGenerationParameters + extends KeyGenerationParameters +{ + private ECDomainParameters domainParams; + + public ECKeyGenerationParameters( + ECDomainParameters domainParams, + SecureRandom random) + { + super(random, domainParams.getN().bitLength()); + + this.domainParams = domainParams; + } + + public ECDomainParameters getDomainParameters() + { + return domainParams; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ECKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ECKeyParameters.java new file mode 100644 index 00000000..19825c5b --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ECKeyParameters.java @@ -0,0 +1,21 @@ +package org.bouncycastle.crypto.params; + +public class ECKeyParameters + extends AsymmetricKeyParameter +{ + ECDomainParameters params; + + protected ECKeyParameters( + boolean isPrivate, + ECDomainParameters params) + { + super(isPrivate); + + this.params = params; + } + + public ECDomainParameters getParameters() + { + return params; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ECPrivateKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ECPrivateKeyParameters.java new file mode 100644 index 00000000..3e49983e --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ECPrivateKeyParameters.java @@ -0,0 +1,22 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class ECPrivateKeyParameters + extends ECKeyParameters +{ + BigInteger d; + + public ECPrivateKeyParameters( + BigInteger d, + ECDomainParameters params) + { + super(true, params); + this.d = d; + } + + public BigInteger getD() + { + return d; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ECPublicKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ECPublicKeyParameters.java new file mode 100644 index 00000000..5fbea19e --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ECPublicKeyParameters.java @@ -0,0 +1,22 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.math.ec.ECPoint; + +public class ECPublicKeyParameters + extends ECKeyParameters +{ + ECPoint Q; + + public ECPublicKeyParameters( + ECPoint Q, + ECDomainParameters params) + { + super(false, params); + this.Q = Q; + } + + public ECPoint getQ() + { + return Q; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ElGamalKeyGenerationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ElGamalKeyGenerationParameters.java new file mode 100644 index 00000000..f5fbabd5 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ElGamalKeyGenerationParameters.java @@ -0,0 +1,30 @@ +package org.bouncycastle.crypto.params; + +import java.security.SecureRandom; + +import org.bouncycastle.crypto.KeyGenerationParameters; + +public class ElGamalKeyGenerationParameters + extends KeyGenerationParameters +{ + private ElGamalParameters params; + + public ElGamalKeyGenerationParameters( + SecureRandom random, + ElGamalParameters params) + { + super(random, getStrength(params)); + + this.params = params; + } + + public ElGamalParameters getParameters() + { + return params; + } + + static int getStrength(ElGamalParameters params) + { + return params.getL() != 0 ? params.getL() : params.getP().bitLength(); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ElGamalKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ElGamalKeyParameters.java new file mode 100644 index 00000000..72506931 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ElGamalKeyParameters.java @@ -0,0 +1,47 @@ +package org.bouncycastle.crypto.params; + + +public class ElGamalKeyParameters + extends AsymmetricKeyParameter +{ + private ElGamalParameters params; + + protected ElGamalKeyParameters( + boolean isPrivate, + ElGamalParameters params) + { + super(isPrivate); + + this.params = params; + } + + public ElGamalParameters getParameters() + { + return params; + } + + public int hashCode() + { + return (params != null) ? params.hashCode() : 0; + } + + public boolean equals( + Object obj) + { + if (!(obj instanceof ElGamalKeyParameters)) + { + return false; + } + + ElGamalKeyParameters dhKey = (ElGamalKeyParameters)obj; + + if (params == null) + { + return dhKey.getParameters() == null; + } + else + { + return params.equals(dhKey.getParameters()); + } + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ElGamalParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ElGamalParameters.java new file mode 100644 index 00000000..166eff35 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ElGamalParameters.java @@ -0,0 +1,69 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +import org.bouncycastle.crypto.CipherParameters; + +public class ElGamalParameters + implements CipherParameters +{ + private BigInteger g; + private BigInteger p; + private int l; + + public ElGamalParameters( + BigInteger p, + BigInteger g) + { + this(p, g, 0); + } + + public ElGamalParameters( + BigInteger p, + BigInteger g, + int l) + { + this.g = g; + this.p = p; + this.l = l; + } + + public BigInteger getP() + { + return p; + } + + /** + * return the generator - g + */ + public BigInteger getG() + { + return g; + } + + /** + * return private value limit - l + */ + public int getL() + { + return l; + } + + public boolean equals( + Object obj) + { + if (!(obj instanceof ElGamalParameters)) + { + return false; + } + + ElGamalParameters pm = (ElGamalParameters)obj; + + return pm.getP().equals(p) && pm.getG().equals(g) && pm.getL() == l; + } + + public int hashCode() + { + return (getP().hashCode() ^ getG().hashCode()) + l; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ElGamalPrivateKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ElGamalPrivateKeyParameters.java new file mode 100644 index 00000000..b8fb529f --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ElGamalPrivateKeyParameters.java @@ -0,0 +1,46 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class ElGamalPrivateKeyParameters + extends ElGamalKeyParameters +{ + private BigInteger x; + + public ElGamalPrivateKeyParameters( + BigInteger x, + ElGamalParameters params) + { + super(true, params); + + this.x = x; + } + + public BigInteger getX() + { + return x; + } + + public boolean equals( + Object obj) + { + if (!(obj instanceof ElGamalPrivateKeyParameters)) + { + return false; + } + + ElGamalPrivateKeyParameters pKey = (ElGamalPrivateKeyParameters)obj; + + if (!pKey.getX().equals(x)) + { + return false; + } + + return super.equals(obj); + } + + public int hashCode() + { + return getX().hashCode(); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ElGamalPublicKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ElGamalPublicKeyParameters.java new file mode 100644 index 00000000..d7da7a93 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ElGamalPublicKeyParameters.java @@ -0,0 +1,41 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class ElGamalPublicKeyParameters + extends ElGamalKeyParameters +{ + private BigInteger y; + + public ElGamalPublicKeyParameters( + BigInteger y, + ElGamalParameters params) + { + super(false, params); + + this.y = y; + } + + public BigInteger getY() + { + return y; + } + + public int hashCode() + { + return y.hashCode() ^ super.hashCode(); + } + + public boolean equals( + Object obj) + { + if (!(obj instanceof ElGamalPublicKeyParameters)) + { + return false; + } + + ElGamalPublicKeyParameters other = (ElGamalPublicKeyParameters)obj; + + return other.getY().equals(y) && super.equals(obj); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/GOST3410KeyGenerationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410KeyGenerationParameters.java new file mode 100644 index 00000000..74e05a96 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410KeyGenerationParameters.java @@ -0,0 +1,25 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.KeyGenerationParameters; + +import java.security.SecureRandom; + +public class GOST3410KeyGenerationParameters + extends KeyGenerationParameters +{ + private GOST3410Parameters params; + + public GOST3410KeyGenerationParameters( + SecureRandom random, + GOST3410Parameters params) + { + super(random, params.getP().bitLength() - 1); + + this.params = params; + } + + public GOST3410Parameters getParameters() + { + return params; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/GOST3410KeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410KeyParameters.java new file mode 100644 index 00000000..6716924a --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410KeyParameters.java @@ -0,0 +1,21 @@ +package org.bouncycastle.crypto.params; + +public class GOST3410KeyParameters + extends AsymmetricKeyParameter +{ + private GOST3410Parameters params; + + public GOST3410KeyParameters( + boolean isPrivate, + GOST3410Parameters params) + { + super(isPrivate); + + this.params = params; + } + + public GOST3410Parameters getParameters() + { + return params; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/GOST3410Parameters.java b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410Parameters.java new file mode 100644 index 00000000..07450f61 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410Parameters.java @@ -0,0 +1,74 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +import java.math.BigInteger; + +public class GOST3410Parameters + implements CipherParameters +{ + private BigInteger p; + private BigInteger q; + private BigInteger a; + private GOST3410ValidationParameters validation; + + public GOST3410Parameters( + BigInteger p, + BigInteger q, + BigInteger a) + { + this.p = p; + this.q = q; + this.a = a; + } + + public GOST3410Parameters( + BigInteger p, + BigInteger q, + BigInteger a, + GOST3410ValidationParameters params) + { + this.a = a; + this.p = p; + this.q = q; + this.validation = params; + } + + public BigInteger getP() + { + return p; + } + + public BigInteger getQ() + { + return q; + } + + public BigInteger getA() + { + return a; + } + + public GOST3410ValidationParameters getValidationParameters() + { + return validation; + } + + public int hashCode() + { + return p.hashCode() ^ q.hashCode() ^ a.hashCode(); + } + + public boolean equals( + Object obj) + { + if (!(obj instanceof GOST3410Parameters)) + { + return false; + } + + GOST3410Parameters pm = (GOST3410Parameters)obj; + + return (pm.getP().equals(p) && pm.getQ().equals(q) && pm.getA().equals(a)); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/GOST3410PrivateKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410PrivateKeyParameters.java new file mode 100644 index 00000000..408e0657 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410PrivateKeyParameters.java @@ -0,0 +1,23 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class GOST3410PrivateKeyParameters + extends GOST3410KeyParameters +{ + private BigInteger x; + + public GOST3410PrivateKeyParameters( + BigInteger x, + GOST3410Parameters params) + { + super(true, params); + + this.x = x; + } + + public BigInteger getX() + { + return x; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/GOST3410PublicKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410PublicKeyParameters.java new file mode 100644 index 00000000..9dfd2d98 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410PublicKeyParameters.java @@ -0,0 +1,23 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class GOST3410PublicKeyParameters + extends GOST3410KeyParameters +{ + private BigInteger y; + + public GOST3410PublicKeyParameters( + BigInteger y, + GOST3410Parameters params) + { + super(false, params); + + this.y = y; + } + + public BigInteger getY() + { + return y; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/GOST3410ValidationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410ValidationParameters.java new file mode 100644 index 00000000..c2a4fb57 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/GOST3410ValidationParameters.java @@ -0,0 +1,84 @@ +package org.bouncycastle.crypto.params; + +public class GOST3410ValidationParameters +{ + private int x0; + private int c; + private long x0L; + private long cL; + + + public GOST3410ValidationParameters( + int x0, + int c) + { + this.x0 = x0; + this.c = c; + } + + public GOST3410ValidationParameters( + long x0L, + long cL) + { + this.x0L = x0L; + this.cL = cL; + } + + public int getC() + { + return c; + } + + public int getX0() + { + return x0; + } + + public long getCL() + { + return cL; + } + + public long getX0L() + { + return x0L; + } + + public boolean equals( + Object o) + { + if (!(o instanceof GOST3410ValidationParameters)) + { + return false; + } + + GOST3410ValidationParameters other = (GOST3410ValidationParameters)o; + + if (other.c != this.c) + { + return false; + } + + if (other.x0 != this.x0) + { + return false; + } + + if (other.cL != this.cL) + { + return false; + } + + if (other.x0L != this.x0L) + { + return false; + } + + return true; + } + + public int hashCode() + { + return x0 ^ c ^ (int) x0L ^ (int)(x0L >> 32) ^ (int) cL ^ (int)(cL >> 32); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/HKDFParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/HKDFParameters.java new file mode 100644 index 00000000..2db3ce61 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/HKDFParameters.java @@ -0,0 +1,123 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.DerivationParameters; +import org.bouncycastle.util.Arrays; + +/** + * Parameter class for the HKDFBytesGenerator class. + */ +public class HKDFParameters + implements DerivationParameters +{ + private final byte[] ikm; + private final boolean skipExpand; + private final byte[] salt; + private final byte[] info; + + private HKDFParameters(final byte[] ikm, final boolean skip, + final byte[] salt, final byte[] info) + { + if (ikm == null) + { + throw new IllegalArgumentException( + "IKM (input keying material) should not be null"); + } + + this.ikm = Arrays.clone(ikm); + + this.skipExpand = skip; + + if (salt == null || salt.length == 0) + { + this.salt = null; + } + else + { + this.salt = Arrays.clone(salt); + } + + if (info == null) + { + this.info = new byte[0]; + } + else + { + this.info = Arrays.clone(info); + } + } + + /** + * Generates parameters for HKDF, specifying both the optional salt and + * optional info. Step 1: Extract won't be skipped. + * + * @param ikm the input keying material or seed + * @param salt the salt to use, may be null for a salt for hashLen zeros + * @param info the info to use, may be null for an info field of zero bytes + */ + public HKDFParameters(final byte[] ikm, final byte[] salt, final byte[] info) + { + this(ikm, false, salt, info); + } + + /** + * Factory method that makes the HKDF skip the extract part of the key + * derivation function. + * + * @param ikm the input keying material or seed, directly used for step 2: + * Expand + * @param info the info to use, may be null for an info field of zero bytes + * @return HKDFParameters that makes the implementation skip step 1 + */ + public static HKDFParameters skipExtractParameters(final byte[] ikm, + final byte[] info) + { + + return new HKDFParameters(ikm, true, null, info); + } + + public static HKDFParameters defaultParameters(final byte[] ikm) + { + return new HKDFParameters(ikm, false, null, null); + } + + /** + * Returns the input keying material or seed. + * + * @return the keying material + */ + public byte[] getIKM() + { + return Arrays.clone(ikm); + } + + /** + * Returns if step 1: extract has to be skipped or not + * + * @return true for skipping, false for no skipping of step 1 + */ + public boolean skipExtract() + { + return skipExpand; + } + + /** + * Returns the salt, or null if the salt should be generated as a byte array + * of HashLen zeros. + * + * @return the salt, or null + */ + public byte[] getSalt() + { + return Arrays.clone(salt); + } + + /** + * Returns the info field, which may be empty (null is converted to empty). + * + * @return the info field, never null + */ + public byte[] getInfo() + { + return Arrays.clone(info); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/IESParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/IESParameters.java new file mode 100644 index 00000000..0600b346 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/IESParameters.java @@ -0,0 +1,44 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +/** + * parameters for using an integrated cipher in stream mode. + */ +public class IESParameters + implements CipherParameters +{ + private byte[] derivation; + private byte[] encoding; + private int macKeySize; + + /** + * @param derivation the derivation parameter for the KDF function. + * @param encoding the encoding parameter for the KDF function. + * @param macKeySize the size of the MAC key (in bits). + */ + public IESParameters( + byte[] derivation, + byte[] encoding, + int macKeySize) + { + this.derivation = derivation; + this.encoding = encoding; + this.macKeySize = macKeySize; + } + + public byte[] getDerivationV() + { + return derivation; + } + + public byte[] getEncodingV() + { + return encoding; + } + + public int getMacKeySize() + { + return macKeySize; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/IESWithCipherParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/IESWithCipherParameters.java new file mode 100644 index 00000000..ef61b2cc --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/IESWithCipherParameters.java @@ -0,0 +1,30 @@ +package org.bouncycastle.crypto.params; + + +public class IESWithCipherParameters + extends IESParameters +{ + private int cipherKeySize; + + /** + * @param derivation the derivation parameter for the KDF function. + * @param encoding the encoding parameter for the KDF function. + * @param macKeySize the size of the MAC key (in bits). + * @param cipherKeySize the size of the associated Cipher key (in bits). + */ + public IESWithCipherParameters( + byte[] derivation, + byte[] encoding, + int macKeySize, + int cipherKeySize) + { + super(derivation, encoding, macKeySize); + + this.cipherKeySize = cipherKeySize; + } + + public int getCipherKeySize() + { + return cipherKeySize; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ISO18033KDFParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ISO18033KDFParameters.java new file mode 100644 index 00000000..8dffe2ee --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ISO18033KDFParameters.java @@ -0,0 +1,23 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.DerivationParameters; + +/** + * parameters for Key derivation functions for ISO-18033 + */ +public class ISO18033KDFParameters + implements DerivationParameters +{ + byte[] seed; + + public ISO18033KDFParameters( + byte[] seed) + { + this.seed = seed; + } + + public byte[] getSeed() + { + return seed; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/KDFParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/KDFParameters.java new file mode 100644 index 00000000..f3bac647 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/KDFParameters.java @@ -0,0 +1,31 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.DerivationParameters; + +/** + * parameters for Key derivation functions for IEEE P1363a + */ +public class KDFParameters + implements DerivationParameters +{ + byte[] iv; + byte[] shared; + + public KDFParameters( + byte[] shared, + byte[] iv) + { + this.shared = shared; + this.iv = iv; + } + + public byte[] getSharedSecret() + { + return shared; + } + + public byte[] getIV() + { + return iv; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/KeyParameter.java b/core/src/main/java/org/bouncycastle/crypto/params/KeyParameter.java new file mode 100644 index 00000000..5c4fe0e0 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/KeyParameter.java @@ -0,0 +1,30 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +public class KeyParameter + implements CipherParameters +{ + private byte[] key; + + public KeyParameter( + byte[] key) + { + this(key, 0, key.length); + } + + public KeyParameter( + byte[] key, + int keyOff, + int keyLen) + { + this.key = new byte[keyLen]; + + System.arraycopy(key, keyOff, this.key, 0, keyLen); + } + + public byte[] getKey() + { + return key; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/MGFParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/MGFParameters.java new file mode 100644 index 00000000..847bd989 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/MGFParameters.java @@ -0,0 +1,32 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.DerivationParameters; + +/** + * parameters for mask derivation functions. + */ +public class MGFParameters + implements DerivationParameters +{ + byte[] seed; + + public MGFParameters( + byte[] seed) + { + this(seed, 0, seed.length); + } + + public MGFParameters( + byte[] seed, + int off, + int len) + { + this.seed = new byte[len]; + System.arraycopy(seed, off, this.seed, 0, len); + } + + public byte[] getSeed() + { + return seed; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/MQVPrivateParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/MQVPrivateParameters.java new file mode 100644 index 00000000..832c07fd --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/MQVPrivateParameters.java @@ -0,0 +1,43 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +public class MQVPrivateParameters + implements CipherParameters +{ + private ECPrivateKeyParameters staticPrivateKey; + private ECPrivateKeyParameters ephemeralPrivateKey; + private ECPublicKeyParameters ephemeralPublicKey; + + public MQVPrivateParameters( + ECPrivateKeyParameters staticPrivateKey, + ECPrivateKeyParameters ephemeralPrivateKey) + { + this(staticPrivateKey, ephemeralPrivateKey, null); + } + + public MQVPrivateParameters( + ECPrivateKeyParameters staticPrivateKey, + ECPrivateKeyParameters ephemeralPrivateKey, + ECPublicKeyParameters ephemeralPublicKey) + { + this.staticPrivateKey = staticPrivateKey; + this.ephemeralPrivateKey = ephemeralPrivateKey; + this.ephemeralPublicKey = ephemeralPublicKey; + } + + public ECPrivateKeyParameters getStaticPrivateKey() + { + return staticPrivateKey; + } + + public ECPrivateKeyParameters getEphemeralPrivateKey() + { + return ephemeralPrivateKey; + } + + public ECPublicKeyParameters getEphemeralPublicKey() + { + return ephemeralPublicKey; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/MQVPublicParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/MQVPublicParameters.java new file mode 100644 index 00000000..b3b24671 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/MQVPublicParameters.java @@ -0,0 +1,28 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +public class MQVPublicParameters + implements CipherParameters +{ + private ECPublicKeyParameters staticPublicKey; + private ECPublicKeyParameters ephemeralPublicKey; + + public MQVPublicParameters( + ECPublicKeyParameters staticPublicKey, + ECPublicKeyParameters ephemeralPublicKey) + { + this.staticPublicKey = staticPublicKey; + this.ephemeralPublicKey = ephemeralPublicKey; + } + + public ECPublicKeyParameters getStaticPublicKey() + { + return staticPublicKey; + } + + public ECPublicKeyParameters getEphemeralPublicKey() + { + return ephemeralPublicKey; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/NaccacheSternKeyGenerationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/NaccacheSternKeyGenerationParameters.java new file mode 100644 index 00000000..758fcd79 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/NaccacheSternKeyGenerationParameters.java @@ -0,0 +1,97 @@ +package org.bouncycastle.crypto.params; + +import java.security.SecureRandom; + +import org.bouncycastle.crypto.KeyGenerationParameters; + +/** + * Parameters for NaccacheStern public private key generation. For details on + * this cipher, please see + * + * http://www.gemplus.com/smart/rd/publications/pdf/NS98pkcs.pdf + */ +public class NaccacheSternKeyGenerationParameters extends KeyGenerationParameters +{ + + // private BigInteger publicExponent; + private int certainty; + + private int cntSmallPrimes; + + private boolean debug = false; + + /** + * Parameters for generating a NaccacheStern KeyPair. + * + * @param random + * The source of randomness + * @param strength + * The desired strength of the Key in Bits + * @param certainty + * the probability that the generated primes are not really prime + * as integer: 2^(-certainty) is then the probability + * @param cntSmallPrimes + * How many small key factors are desired + */ + public NaccacheSternKeyGenerationParameters(SecureRandom random, int strength, int certainty, int cntSmallPrimes) + { + this(random, strength, certainty, cntSmallPrimes, false); + } + + /** + * Parameters for a NaccacheStern KeyPair. + * + * @param random + * The source of randomness + * @param strength + * The desired strength of the Key in Bits + * @param certainty + * the probability that the generated primes are not really prime + * as integer: 2^(-certainty) is then the probability + * @param cntSmallPrimes + * How many small key factors are desired + * @param debug + * Turn debugging on or off (reveals secret information, use with + * caution) + */ + public NaccacheSternKeyGenerationParameters(SecureRandom random, + int strength, int certainty, int cntSmallPrimes, boolean debug) + { + super(random, strength); + + this.certainty = certainty; + if (cntSmallPrimes % 2 == 1) + { + throw new IllegalArgumentException("cntSmallPrimes must be a multiple of 2"); + } + if (cntSmallPrimes < 30) + { + throw new IllegalArgumentException("cntSmallPrimes must be >= 30 for security reasons"); + } + this.cntSmallPrimes = cntSmallPrimes; + + this.debug = debug; + } + + /** + * @return Returns the certainty. + */ + public int getCertainty() + { + return certainty; + } + + /** + * @return Returns the cntSmallPrimes. + */ + public int getCntSmallPrimes() + { + return cntSmallPrimes; + } + + public boolean isDebug() + { + return debug; + } + +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/NaccacheSternKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/NaccacheSternKeyParameters.java new file mode 100644 index 00000000..21b6a286 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/NaccacheSternKeyParameters.java @@ -0,0 +1,53 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +/** + * Public key parameters for NaccacheStern cipher. For details on this cipher, + * please see + * + * http://www.gemplus.com/smart/rd/publications/pdf/NS98pkcs.pdf + */ +public class NaccacheSternKeyParameters extends AsymmetricKeyParameter +{ + + private BigInteger g, n; + + int lowerSigmaBound; + + /** + * @param privateKey + */ + public NaccacheSternKeyParameters(boolean privateKey, BigInteger g, BigInteger n, int lowerSigmaBound) + { + super(privateKey); + this.g = g; + this.n = n; + this.lowerSigmaBound = lowerSigmaBound; + } + + /** + * @return Returns the g. + */ + public BigInteger getG() + { + return g; + } + + /** + * @return Returns the lowerSigmaBound. + */ + public int getLowerSigmaBound() + { + return lowerSigmaBound; + } + + /** + * @return Returns the n. + */ + public BigInteger getModulus() + { + return n; + } + +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/NaccacheSternPrivateKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/NaccacheSternPrivateKeyParameters.java new file mode 100644 index 00000000..6d0ec486 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/NaccacheSternPrivateKeyParameters.java @@ -0,0 +1,50 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; +import java.util.Vector; + +/** + * Private key parameters for NaccacheStern cipher. For details on this cipher, + * please see + * + * http://www.gemplus.com/smart/rd/publications/pdf/NS98pkcs.pdf + */ +public class NaccacheSternPrivateKeyParameters extends NaccacheSternKeyParameters +{ + private BigInteger phi_n; + private Vector smallPrimes; + + /** + * Constructs a NaccacheSternPrivateKey + * + * @param g + * the public enryption parameter g + * @param n + * the public modulus n = p*q + * @param lowerSigmaBound + * the public lower sigma bound up to which data can be encrypted + * @param smallPrimes + * the small primes, of which sigma is constructed in the right + * order + * @param phi_n + * the private modulus phi(n) = (p-1)(q-1) + */ + public NaccacheSternPrivateKeyParameters(BigInteger g, BigInteger n, + int lowerSigmaBound, Vector smallPrimes, + BigInteger phi_n) + { + super(true, g, n, lowerSigmaBound); + this.smallPrimes = smallPrimes; + this.phi_n = phi_n; + } + + public BigInteger getPhi_n() + { + return phi_n; + } + + public Vector getSmallPrimes() + { + return smallPrimes; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithIV.java b/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithIV.java new file mode 100644 index 00000000..4a1e6e9a --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithIV.java @@ -0,0 +1,39 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +public class ParametersWithIV + implements CipherParameters +{ + private byte[] iv; + private CipherParameters parameters; + + public ParametersWithIV( + CipherParameters parameters, + byte[] iv) + { + this(parameters, iv, 0, iv.length); + } + + public ParametersWithIV( + CipherParameters parameters, + byte[] iv, + int ivOff, + int ivLen) + { + this.iv = new byte[ivLen]; + this.parameters = parameters; + + System.arraycopy(iv, ivOff, this.iv, 0, ivLen); + } + + public byte[] getIV() + { + return iv; + } + + public CipherParameters getParameters() + { + return parameters; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithRandom.java b/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithRandom.java new file mode 100644 index 00000000..a7b18e51 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithRandom.java @@ -0,0 +1,36 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +import java.security.SecureRandom; + +public class ParametersWithRandom + implements CipherParameters +{ + private SecureRandom random; + private CipherParameters parameters; + + public ParametersWithRandom( + CipherParameters parameters, + SecureRandom random) + { + this.random = random; + this.parameters = parameters; + } + + public ParametersWithRandom( + CipherParameters parameters) + { + this(parameters, new SecureRandom()); + } + + public SecureRandom getRandom() + { + return random; + } + + public CipherParameters getParameters() + { + return parameters; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithSBox.java b/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithSBox.java new file mode 100644 index 00000000..b226a9d9 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithSBox.java @@ -0,0 +1,28 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +public class ParametersWithSBox + implements CipherParameters +{ + private CipherParameters parameters; + private byte[] sBox; + + public ParametersWithSBox( + CipherParameters parameters, + byte[] sBox) + { + this.parameters = parameters; + this.sBox = sBox; + } + + public byte[] getSBox() + { + return sBox; + } + + public CipherParameters getParameters() + { + return parameters; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithSalt.java b/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithSalt.java new file mode 100644 index 00000000..73765dd5 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/ParametersWithSalt.java @@ -0,0 +1,42 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +/** + * Cipher parameters with a fixed salt value associated with them. + */ +public class ParametersWithSalt + implements CipherParameters +{ + private byte[] salt; + private CipherParameters parameters; + + public ParametersWithSalt( + CipherParameters parameters, + byte[] salt) + { + this(parameters, salt, 0, salt.length); + } + + public ParametersWithSalt( + CipherParameters parameters, + byte[] salt, + int saltOff, + int saltLen) + { + this.salt = new byte[saltLen]; + this.parameters = parameters; + + System.arraycopy(salt, saltOff, this.salt, 0, saltLen); + } + + public byte[] getSalt() + { + return salt; + } + + public CipherParameters getParameters() + { + return parameters; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/RC2Parameters.java b/core/src/main/java/org/bouncycastle/crypto/params/RC2Parameters.java new file mode 100644 index 00000000..dc33ec5b --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/RC2Parameters.java @@ -0,0 +1,36 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +public class RC2Parameters + implements CipherParameters +{ + private byte[] key; + private int bits; + + public RC2Parameters( + byte[] key) + { + this(key, (key.length > 128) ? 1024 : (key.length * 8)); + } + + public RC2Parameters( + byte[] key, + int bits) + { + this.key = new byte[key.length]; + this.bits = bits; + + System.arraycopy(key, 0, this.key, 0, key.length); + } + + public byte[] getKey() + { + return key; + } + + public int getEffectiveKeyBits() + { + return bits; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/RC5Parameters.java b/core/src/main/java/org/bouncycastle/crypto/params/RC5Parameters.java new file mode 100644 index 00000000..6cbd57f1 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/RC5Parameters.java @@ -0,0 +1,35 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +public class RC5Parameters + implements CipherParameters +{ + private byte[] key; + private int rounds; + + public RC5Parameters( + byte[] key, + int rounds) + { + if (key.length > 255) + { + throw new IllegalArgumentException("RC5 key length can be no greater than 255"); + } + + this.key = new byte[key.length]; + this.rounds = rounds; + + System.arraycopy(key, 0, this.key, 0, key.length); + } + + public byte[] getKey() + { + return key; + } + + public int getRounds() + { + return rounds; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/RSABlindingParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/RSABlindingParameters.java new file mode 100644 index 00000000..c7fa6ba6 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/RSABlindingParameters.java @@ -0,0 +1,35 @@ +package org.bouncycastle.crypto.params; + +import org.bouncycastle.crypto.CipherParameters; + +import java.math.BigInteger; + +public class RSABlindingParameters + implements CipherParameters +{ + private RSAKeyParameters publicKey; + private BigInteger blindingFactor; + + public RSABlindingParameters( + RSAKeyParameters publicKey, + BigInteger blindingFactor) + { + if (publicKey instanceof RSAPrivateCrtKeyParameters) + { + throw new IllegalArgumentException("RSA parameters should be for a public key"); + } + + this.publicKey = publicKey; + this.blindingFactor = blindingFactor; + } + + public RSAKeyParameters getPublicKey() + { + return publicKey; + } + + public BigInteger getBlindingFactor() + { + return blindingFactor; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/RSAKeyGenerationParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/RSAKeyGenerationParameters.java new file mode 100644 index 00000000..38b55fcd --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/RSAKeyGenerationParameters.java @@ -0,0 +1,48 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; +import java.security.SecureRandom; + +import org.bouncycastle.crypto.KeyGenerationParameters; + +public class RSAKeyGenerationParameters + extends KeyGenerationParameters +{ + private BigInteger publicExponent; + private int certainty; + + public RSAKeyGenerationParameters( + BigInteger publicExponent, + SecureRandom random, + int strength, + int certainty) + { + super(random, strength); + + if (strength < 12) + { + throw new IllegalArgumentException("key strength too small"); + } + + // + // public exponent cannot be even + // + if (!publicExponent.testBit(0)) + { + throw new IllegalArgumentException("public exponent cannot be even"); + } + + this.publicExponent = publicExponent; + this.certainty = certainty; + } + + public BigInteger getPublicExponent() + { + return publicExponent; + } + + public int getCertainty() + { + return certainty; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/RSAKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/RSAKeyParameters.java new file mode 100644 index 00000000..4a2d9354 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/RSAKeyParameters.java @@ -0,0 +1,31 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class RSAKeyParameters + extends AsymmetricKeyParameter +{ + private BigInteger modulus; + private BigInteger exponent; + + public RSAKeyParameters( + boolean isPrivate, + BigInteger modulus, + BigInteger exponent) + { + super(isPrivate); + + this.modulus = modulus; + this.exponent = exponent; + } + + public BigInteger getModulus() + { + return modulus; + } + + public BigInteger getExponent() + { + return exponent; + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/params/RSAPrivateCrtKeyParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/RSAPrivateCrtKeyParameters.java new file mode 100644 index 00000000..b61cb5c4 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/params/RSAPrivateCrtKeyParameters.java @@ -0,0 +1,67 @@ +package org.bouncycastle.crypto.params; + +import java.math.BigInteger; + +public class RSAPrivateCrtKeyParameters + extends RSAKeyParameters +{ + private BigInteger e; + private BigInteger p; + private BigInteger q; + private BigInteger dP; + private BigInteger dQ; + private BigInteger qInv; + + /** + * + */ + public RSAPrivateCrtKeyParameters( + BigInteger modulus, + BigInteger publicExponent, + BigInteger privateExponent, + BigInteger p, + BigInteger q, + BigInteger dP, + BigInteger dQ, + BigInteger qInv) + { + super(true, modulus, privateExponent); + + this.e = publicExponent; + this.p = p; + this.q = q; + this.dP = dP; + this.dQ = dQ; + this.qInv = qInv; + } + + public BigInteger getPublicExponent() + { + return e; + } + + public BigInteger getP() + { + return p; + } + + public BigInteger getQ() + { + return q; + } + + public BigInteger getDP() + { + return dP; + } + + public BigInteger getDQ() + { + return dQ; + } + + public BigInteger getQInv() + { + return qInv; + } +} |