diff options
Diffstat (limited to 'core/src/main/java/org/spongycastle/crypto/params')
67 files changed, 3529 insertions, 0 deletions
diff --git a/core/src/main/java/org/spongycastle/crypto/params/AEADParameters.java b/core/src/main/java/org/spongycastle/crypto/params/AEADParameters.java new file mode 100644 index 00000000..b3ee4fca --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/AEADParameters.java @@ -0,0 +1,60 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/AsymmetricKeyParameter.java b/core/src/main/java/org/spongycastle/crypto/params/AsymmetricKeyParameter.java new file mode 100644 index 00000000..9ac1c48b --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/AsymmetricKeyParameter.java @@ -0,0 +1,20 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/CCMParameters.java b/core/src/main/java/org/spongycastle/crypto/params/CCMParameters.java new file mode 100644 index 00000000..a1198645 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/CCMParameters.java @@ -0,0 +1,21 @@ +package org.spongycastle.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/spongycastle/crypto/params/CramerShoupKeyGenerationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/CramerShoupKeyGenerationParameters.java new file mode 100644 index 00000000..38976d2c --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/CramerShoupKeyGenerationParameters.java @@ -0,0 +1,24 @@ +package org.spongycastle.crypto.params; + +import java.security.SecureRandom; + +import org.spongycastle.crypto.KeyGenerationParameters; + +public class CramerShoupKeyGenerationParameters extends KeyGenerationParameters { + + private CramerShoupParameters params; + + public CramerShoupKeyGenerationParameters(SecureRandom random, CramerShoupParameters params) { + super(random, getStrength(params)); + + this.params = params; + } + + public CramerShoupParameters getParameters() { + return params; + } + + static int getStrength(CramerShoupParameters params) { + return params.getP().bitLength(); + } +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/CramerShoupKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/CramerShoupKeyParameters.java new file mode 100644 index 00000000..5bacd588 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/CramerShoupKeyParameters.java @@ -0,0 +1,40 @@ +package org.spongycastle.crypto.params; + +public class CramerShoupKeyParameters extends AsymmetricKeyParameter { + + private CramerShoupParameters params; + + protected CramerShoupKeyParameters(boolean isPrivate, CramerShoupParameters params) { + super(isPrivate); + + this.params = params; + } + + public CramerShoupParameters getParameters() { + return params; + } + + public boolean equals(Object obj) { + if (!(obj instanceof CramerShoupKeyParameters)) { + return false; + } + + CramerShoupKeyParameters csKey = (CramerShoupKeyParameters) obj; + + if (params == null) { + return csKey.getParameters() == null; + } else { + return params.equals(csKey.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/spongycastle/crypto/params/CramerShoupParameters.java b/core/src/main/java/org/spongycastle/crypto/params/CramerShoupParameters.java new file mode 100644 index 00000000..d1504bab --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/CramerShoupParameters.java @@ -0,0 +1,53 @@ +package org.spongycastle.crypto.params; + +import java.math.BigInteger; + +import org.spongycastle.crypto.CipherParameters; +import org.spongycastle.crypto.Digest; + +public class CramerShoupParameters implements CipherParameters { + + private BigInteger p; // prime order of G + private BigInteger g1, g2; // generate G + + private Digest H; // hash function + + public CramerShoupParameters(BigInteger p, BigInteger g1, BigInteger g2, Digest H) { + this.p = p; + this.g1 = g1; + this.g2 = g2; + this.H = H; + } + + public boolean equals(Object obj) { + if (!(obj instanceof DSAParameters)) { + return false; + } + + CramerShoupParameters pm = (CramerShoupParameters) obj; + + return (pm.getP().equals(p) && pm.getG1().equals(g1) && pm.getG2().equals(g2)); + } + + public int hashCode() { + return getP().hashCode() ^ getG1().hashCode() ^ getG2().hashCode(); + } + + public BigInteger getG1() { + return g1; + } + + public BigInteger getG2() { + return g2; + } + + public BigInteger getP() { + return p; + } + + public Digest getH() { + H.reset(); + return H; + } + +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/CramerShoupPrivateKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/CramerShoupPrivateKeyParameters.java new file mode 100644 index 00000000..6aae8be2 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/CramerShoupPrivateKeyParameters.java @@ -0,0 +1,61 @@ +package org.spongycastle.crypto.params; + +import java.math.BigInteger; + +public class CramerShoupPrivateKeyParameters extends CramerShoupKeyParameters { + + private BigInteger x1, x2, y1, y2, z; // Z_p + private CramerShoupPublicKeyParameters pk; // public key + + public CramerShoupPrivateKeyParameters(CramerShoupParameters params, BigInteger x1, BigInteger x2, BigInteger y1, BigInteger y2, BigInteger z) { + super(true, params); + + this.x1 = x1; + this.x2 = x2; + this.y1 = y1; + this.y2 = y2; + this.z = z; + } + + public BigInteger getX1() { + return x1; + } + + public BigInteger getX2() { + return x2; + } + + public BigInteger getY1() { + return y1; + } + + public BigInteger getY2() { + return y2; + } + + public BigInteger getZ() { + return z; + } + + public void setPk(CramerShoupPublicKeyParameters pk) { + this.pk = pk; + } + + public CramerShoupPublicKeyParameters getPk() { + return pk; + } + + public int hashCode() { + return x1.hashCode() ^ x2.hashCode() ^ y1.hashCode() ^ y2.hashCode() ^ z.hashCode() ^ super.hashCode(); + } + + public boolean equals(Object obj) { + if (!(obj instanceof CramerShoupPrivateKeyParameters)) { + return false; + } + + CramerShoupPrivateKeyParameters other = (CramerShoupPrivateKeyParameters) obj; + + return other.getX1().equals(this.x1) && other.getX2().equals(this.x2) && other.getY1().equals(this.y1) && other.getY2().equals(this.y2) && other.getZ().equals(this.z) && super.equals(obj); + } +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/CramerShoupPublicKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/CramerShoupPublicKeyParameters.java new file mode 100644 index 00000000..20752bf6 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/CramerShoupPublicKeyParameters.java @@ -0,0 +1,42 @@ +package org.spongycastle.crypto.params; + +import java.math.BigInteger; + +public class CramerShoupPublicKeyParameters extends CramerShoupKeyParameters { + + private BigInteger c, d, h; // public key group elements + + public CramerShoupPublicKeyParameters(CramerShoupParameters params, BigInteger c, BigInteger d, BigInteger h) { + super(false, params); + + this.c = c; + this.d = d; + this.h = h; + } + + public BigInteger getC() { + return c; + } + + public BigInteger getD() { + return d; + } + + public BigInteger getH() { + return h; + } + + public int hashCode() { + return c.hashCode() ^ d.hashCode() ^ h.hashCode() ^ super.hashCode(); + } + + public boolean equals(Object obj) { + if (!(obj instanceof CramerShoupPublicKeyParameters)) { + return false; + } + + CramerShoupPublicKeyParameters other = (CramerShoupPublicKeyParameters) obj; + + return other.getC().equals(c) && other.getD().equals(d) && other.getH().equals(h) && super.equals(obj); + } +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/DESParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DESParameters.java new file mode 100644 index 00000000..6c9c3985 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DESParameters.java @@ -0,0 +1,107 @@ +package org.spongycastle.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/spongycastle/crypto/params/DESedeParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DESedeParameters.java new file mode 100644 index 00000000..498371ad --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DESedeParameters.java @@ -0,0 +1,57 @@ +package org.spongycastle.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/spongycastle/crypto/params/DHKeyGenerationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DHKeyGenerationParameters.java new file mode 100644 index 00000000..246a195e --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DHKeyGenerationParameters.java @@ -0,0 +1,30 @@ +package org.spongycastle.crypto.params; + +import java.security.SecureRandom; + +import org.spongycastle.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/spongycastle/crypto/params/DHKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DHKeyParameters.java new file mode 100644 index 00000000..f2ac392e --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DHKeyParameters.java @@ -0,0 +1,54 @@ +package org.spongycastle.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/spongycastle/crypto/params/DHParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DHParameters.java new file mode 100644 index 00000000..d4f03ff3 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DHParameters.java @@ -0,0 +1,189 @@ +package org.spongycastle.crypto.params; + +import java.math.BigInteger; + +import org.spongycastle.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/spongycastle/crypto/params/DHPrivateKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DHPrivateKeyParameters.java new file mode 100644 index 00000000..6f192b36 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DHPrivateKeyParameters.java @@ -0,0 +1,41 @@ +package org.spongycastle.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/spongycastle/crypto/params/DHPublicKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DHPublicKeyParameters.java new file mode 100644 index 00000000..3fbdd58e --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DHPublicKeyParameters.java @@ -0,0 +1,41 @@ +package org.spongycastle.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/spongycastle/crypto/params/DHValidationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DHValidationParameters.java new file mode 100644 index 00000000..cf385d0c --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DHValidationParameters.java @@ -0,0 +1,50 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/DSAKeyGenerationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DSAKeyGenerationParameters.java new file mode 100644 index 00000000..eaa8f80d --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DSAKeyGenerationParameters.java @@ -0,0 +1,25 @@ +package org.spongycastle.crypto.params; + +import java.security.SecureRandom; + +import org.spongycastle.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/spongycastle/crypto/params/DSAKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DSAKeyParameters.java new file mode 100644 index 00000000..8417a4e9 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DSAKeyParameters.java @@ -0,0 +1,21 @@ +package org.spongycastle.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/spongycastle/crypto/params/DSAParameterGenerationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DSAParameterGenerationParameters.java new file mode 100644 index 00000000..8822209c --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DSAParameterGenerationParameters.java @@ -0,0 +1,80 @@ +package org.spongycastle.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/spongycastle/crypto/params/DSAParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DSAParameters.java new file mode 100644 index 00000000..b1baf067 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DSAParameters.java @@ -0,0 +1,74 @@ +package org.spongycastle.crypto.params; + +import java.math.BigInteger; + +import org.spongycastle.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/spongycastle/crypto/params/DSAPrivateKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DSAPrivateKeyParameters.java new file mode 100644 index 00000000..bd1d9681 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DSAPrivateKeyParameters.java @@ -0,0 +1,23 @@ +package org.spongycastle.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/spongycastle/crypto/params/DSAPublicKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DSAPublicKeyParameters.java new file mode 100644 index 00000000..cd244b9d --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DSAPublicKeyParameters.java @@ -0,0 +1,23 @@ +package org.spongycastle.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/spongycastle/crypto/params/DSAValidationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/DSAValidationParameters.java new file mode 100644 index 00000000..ca7c6cf6 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/DSAValidationParameters.java @@ -0,0 +1,65 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/ECDomainParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ECDomainParameters.java new file mode 100644 index 00000000..57daaee3 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ECDomainParameters.java @@ -0,0 +1,74 @@ +package org.spongycastle.crypto.params; + +import java.math.BigInteger; + +import org.spongycastle.math.ec.ECConstants; +import org.spongycastle.math.ec.ECCurve; +import org.spongycastle.math.ec.ECPoint; +import org.spongycastle.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.normalize(); + 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/spongycastle/crypto/params/ECKeyGenerationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ECKeyGenerationParameters.java new file mode 100644 index 00000000..4675c65a --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ECKeyGenerationParameters.java @@ -0,0 +1,25 @@ +package org.spongycastle.crypto.params; + +import java.security.SecureRandom; + +import org.spongycastle.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/spongycastle/crypto/params/ECKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ECKeyParameters.java new file mode 100644 index 00000000..480a969f --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ECKeyParameters.java @@ -0,0 +1,21 @@ +package org.spongycastle.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/spongycastle/crypto/params/ECNamedDomainParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ECNamedDomainParameters.java new file mode 100644 index 00000000..b6bb23c1 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ECNamedDomainParameters.java @@ -0,0 +1,35 @@ +package org.spongycastle.crypto.params; + +import java.math.BigInteger; + +import org.spongycastle.asn1.ASN1ObjectIdentifier; +import org.spongycastle.math.ec.ECCurve; +import org.spongycastle.math.ec.ECPoint; + +public class ECNamedDomainParameters + extends ECDomainParameters +{ + private ASN1ObjectIdentifier name; + + public ECNamedDomainParameters(ASN1ObjectIdentifier name, ECCurve curve, ECPoint G, BigInteger n) + { + this(name, curve, G, n, null, null); + } + + public ECNamedDomainParameters(ASN1ObjectIdentifier name, ECCurve curve, ECPoint G, BigInteger n, BigInteger h) + { + this(name, curve, G, n, h, null); + } + + public ECNamedDomainParameters(ASN1ObjectIdentifier name, ECCurve curve, ECPoint G, BigInteger n, BigInteger h, byte[] seed) + { + super(curve, G, n, h, seed); + + this.name = name; + } + + public ASN1ObjectIdentifier getName() + { + return name; + } +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/ECPrivateKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ECPrivateKeyParameters.java new file mode 100644 index 00000000..484ca232 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ECPrivateKeyParameters.java @@ -0,0 +1,22 @@ +package org.spongycastle.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/spongycastle/crypto/params/ECPublicKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ECPublicKeyParameters.java new file mode 100644 index 00000000..deab005c --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ECPublicKeyParameters.java @@ -0,0 +1,22 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.math.ec.ECPoint; + +public class ECPublicKeyParameters + extends ECKeyParameters +{ + ECPoint Q; + + public ECPublicKeyParameters( + ECPoint Q, + ECDomainParameters params) + { + super(false, params); + this.Q = Q.normalize(); + } + + public ECPoint getQ() + { + return Q; + } +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/ElGamalKeyGenerationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ElGamalKeyGenerationParameters.java new file mode 100644 index 00000000..c1741ec1 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ElGamalKeyGenerationParameters.java @@ -0,0 +1,30 @@ +package org.spongycastle.crypto.params; + +import java.security.SecureRandom; + +import org.spongycastle.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/spongycastle/crypto/params/ElGamalKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ElGamalKeyParameters.java new file mode 100644 index 00000000..83c00de6 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ElGamalKeyParameters.java @@ -0,0 +1,47 @@ +package org.spongycastle.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/spongycastle/crypto/params/ElGamalParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ElGamalParameters.java new file mode 100644 index 00000000..60c484b1 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ElGamalParameters.java @@ -0,0 +1,69 @@ +package org.spongycastle.crypto.params; + +import java.math.BigInteger; + +import org.spongycastle.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/spongycastle/crypto/params/ElGamalPrivateKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ElGamalPrivateKeyParameters.java new file mode 100644 index 00000000..51095891 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ElGamalPrivateKeyParameters.java @@ -0,0 +1,46 @@ +package org.spongycastle.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/spongycastle/crypto/params/ElGamalPublicKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ElGamalPublicKeyParameters.java new file mode 100644 index 00000000..ae21f8e8 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ElGamalPublicKeyParameters.java @@ -0,0 +1,41 @@ +package org.spongycastle.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/spongycastle/crypto/params/GOST3410KeyGenerationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/GOST3410KeyGenerationParameters.java new file mode 100644 index 00000000..2a32389f --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/GOST3410KeyGenerationParameters.java @@ -0,0 +1,25 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/GOST3410KeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/GOST3410KeyParameters.java new file mode 100644 index 00000000..b9a6ee7e --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/GOST3410KeyParameters.java @@ -0,0 +1,21 @@ +package org.spongycastle.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/spongycastle/crypto/params/GOST3410Parameters.java b/core/src/main/java/org/spongycastle/crypto/params/GOST3410Parameters.java new file mode 100644 index 00000000..ad29ed65 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/GOST3410Parameters.java @@ -0,0 +1,74 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/GOST3410PrivateKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/GOST3410PrivateKeyParameters.java new file mode 100644 index 00000000..531870e2 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/GOST3410PrivateKeyParameters.java @@ -0,0 +1,23 @@ +package org.spongycastle.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/spongycastle/crypto/params/GOST3410PublicKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/GOST3410PublicKeyParameters.java new file mode 100644 index 00000000..b0b05899 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/GOST3410PublicKeyParameters.java @@ -0,0 +1,23 @@ +package org.spongycastle.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/spongycastle/crypto/params/GOST3410ValidationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/GOST3410ValidationParameters.java new file mode 100644 index 00000000..46f893ca --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/GOST3410ValidationParameters.java @@ -0,0 +1,84 @@ +package org.spongycastle.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/spongycastle/crypto/params/HKDFParameters.java b/core/src/main/java/org/spongycastle/crypto/params/HKDFParameters.java new file mode 100644 index 00000000..94595a15 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/HKDFParameters.java @@ -0,0 +1,123 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.crypto.DerivationParameters; +import org.spongycastle.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/spongycastle/crypto/params/IESParameters.java b/core/src/main/java/org/spongycastle/crypto/params/IESParameters.java new file mode 100644 index 00000000..419d135d --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/IESParameters.java @@ -0,0 +1,44 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/IESWithCipherParameters.java b/core/src/main/java/org/spongycastle/crypto/params/IESWithCipherParameters.java new file mode 100644 index 00000000..68bb2d62 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/IESWithCipherParameters.java @@ -0,0 +1,30 @@ +package org.spongycastle.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/spongycastle/crypto/params/ISO18033KDFParameters.java b/core/src/main/java/org/spongycastle/crypto/params/ISO18033KDFParameters.java new file mode 100644 index 00000000..c6e1d999 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ISO18033KDFParameters.java @@ -0,0 +1,23 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/KDFCounterParameters.java b/core/src/main/java/org/spongycastle/crypto/params/KDFCounterParameters.java new file mode 100644 index 00000000..02ee9142 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/KDFCounterParameters.java @@ -0,0 +1,120 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.crypto.DerivationParameters; +import org.spongycastle.util.Arrays; + +/** + * This KDF has been defined by the publicly available NIST SP 800-108 specification. + * NIST SP800-108 allows for alternative orderings of the input fields, meaning that the input can be formated in multiple ways. + * There are 3 supported formats: - Below [i]_2 is a counter of r-bits length concatenated to the fixedInputData. + * <ul> + * <li>1: K(i) := PRF( KI, [i]_2 || Label || 0x00 || Context || [L]_2 ) with the counter at the very beginning of the fixedInputData (The default implementation has this format)</li> + * <li>2: K(i) := PRF( KI, Label || 0x00 || Context || [L]_2 || [i]_2 ) with the counter at the very end of the fixedInputData</li> + * <li>3a: K(i) := PRF( KI, Label || 0x00 || [i]_2 || Context || [L]_2 ) OR:</li> + * <li>3b: K(i) := PRF( KI, Label || 0x00 || [i]_2 || [L]_2 || Context ) OR:</li> + * <li>3c: K(i) := PRF( KI, Label || [i]_2 || 0x00 || Context || [L]_2 ) etc... with the counter somewhere in the 'middle' of the fixedInputData.</li> + * </ul> + * <p> + * This function must be called with the following KDFCounterParameters(): + * - KI <br/> + * - The part of the fixedInputData that comes BEFORE the counter OR null <br/> + * - the part of the fixedInputData that comes AFTER the counter OR null <br/> + * - the length of the counter in bits (not bytes) <br/> + * </p> + * Resulting function calls assuming an 8 bit counter. + * <ul> + * <li>1. KDFCounterParameters(ki, null, "Label || 0x00 || Context || [L]_2]", 8); </li> + * <li>2. KDFCounterParameters(ki, "Label || 0x00 || Context || [L]_2]", null, 8); </li> + * <li>3a. KDFCounterParameters(ki, "Label || 0x00", "Context || [L]_2]", 8); </li> + * <li>3b. KDFCounterParameters(ki, "Label || 0x00", "[L]_2] || Context", 8);</li> + * <li>3c. KDFCounterParameters(ki, "Label", "0x00 || Context || [L]_2]", 8); </li> + * </ul> + */ +public final class KDFCounterParameters + implements DerivationParameters +{ + + private byte[] ki; + private byte[] fixedInputDataCounterPrefix; + private byte[] fixedInputDataCounterSuffix; + private int r; + + /** + * Base constructor - suffix fixed input data only. + * + * @param ki the KDF seed + * @param fixedInputDataCounterSuffix fixed input data to follow counter. + * @param r length of the counter in bits. + */ + public KDFCounterParameters(byte[] ki, byte[] fixedInputDataCounterSuffix, int r) + { + this(ki, null, fixedInputDataCounterSuffix, r); + } + + /** + * Base constructor - prefix and suffix fixed input data. + * + * @param ki the KDF seed + * @param fixedInputDataCounterPrefix fixed input data to precede counter + * @param fixedInputDataCounterSuffix fixed input data to follow counter. + * @param r length of the counter in bits. + */ + public KDFCounterParameters(byte[] ki, byte[] fixedInputDataCounterPrefix, byte[] fixedInputDataCounterSuffix, int r) + { + if (ki == null) + { + throw new IllegalArgumentException("A KDF requires Ki (a seed) as input"); + } + this.ki = Arrays.clone(ki); + + if (fixedInputDataCounterPrefix == null) + { + this.fixedInputDataCounterPrefix = new byte[0]; + } + else + { + this.fixedInputDataCounterPrefix = Arrays.clone(fixedInputDataCounterPrefix); + } + + if (fixedInputDataCounterSuffix == null) + { + this.fixedInputDataCounterSuffix = new byte[0]; + } + else + { + this.fixedInputDataCounterSuffix = Arrays.clone(fixedInputDataCounterSuffix); + } + + if (r != 8 && r != 16 && r != 24 && r != 32) + { + throw new IllegalArgumentException("Length of counter should be 8, 16, 24 or 32"); + } + this.r = r; + } + + public byte[] getKI() + { + return ki; + } + + public byte[] getFixedInputData() + { + //Retained for backwards compatibility + return Arrays.clone(fixedInputDataCounterSuffix); + } + + public byte[] getFixedInputDataCounterPrefix() + { + return Arrays.clone(fixedInputDataCounterPrefix); + } + + public byte[] getFixedInputDataCounterSuffix() + { + return Arrays.clone(fixedInputDataCounterSuffix); + } + + public int getR() + { + return r; + } +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/KDFDoublePipelineIterationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/KDFDoublePipelineIterationParameters.java new file mode 100644 index 00000000..30fe6138 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/KDFDoublePipelineIterationParameters.java @@ -0,0 +1,80 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.crypto.DerivationParameters; +import org.spongycastle.util.Arrays; + +/** + * Note that counter is only supported at the location presented in the + * NIST SP 800-108 specification, not in the additional locations present + * in the CAVP test vectors. + */ +public final class KDFDoublePipelineIterationParameters + implements DerivationParameters +{ + + // could be any valid value, using 32, don't know why + private static final int UNUSED_R = 32; + + private final byte[] ki; + private final boolean useCounter; + private final int r; + private final byte[] fixedInputData; + + private KDFDoublePipelineIterationParameters(byte[] ki, byte[] fixedInputData, int r, boolean useCounter) + { + if (ki == null) + { + throw new IllegalArgumentException("A KDF requires Ki (a seed) as input"); + } + this.ki = Arrays.clone(ki); + + if (fixedInputData == null) + { + this.fixedInputData = new byte[0]; + } + else + { + this.fixedInputData = Arrays.clone(fixedInputData); + } + + if (r != 8 && r != 16 && r != 24 && r != 32) + { + throw new IllegalArgumentException("Length of counter should be 8, 16, 24 or 32"); + } + this.r = r; + + this.useCounter = useCounter; + } + + public static KDFDoublePipelineIterationParameters createWithCounter( + byte[] ki, byte[] fixedInputData, int r) + { + return new KDFDoublePipelineIterationParameters(ki, fixedInputData, r, true); + } + + public static KDFDoublePipelineIterationParameters createWithoutCounter( + byte[] ki, byte[] fixedInputData) + { + return new KDFDoublePipelineIterationParameters(ki, fixedInputData, UNUSED_R, false); + } + + public byte[] getKI() + { + return ki; + } + + public boolean useCounter() + { + return useCounter; + } + + public int getR() + { + return r; + } + + public byte[] getFixedInputData() + { + return Arrays.clone(fixedInputData); + } +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/KDFFeedbackParameters.java b/core/src/main/java/org/spongycastle/crypto/params/KDFFeedbackParameters.java new file mode 100644 index 00000000..44d1e964 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/KDFFeedbackParameters.java @@ -0,0 +1,96 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.crypto.DerivationParameters; +import org.spongycastle.util.Arrays; + +/** + * Note that counter is only supported at the location presented in the + * NIST SP 800-108 specification, not in the additional locations present + * in the CAVP test vectors. + */ +public final class KDFFeedbackParameters + implements DerivationParameters +{ + + // could be any valid value, using 32, don't know why + private static final int UNUSED_R = -1; + + private final byte[] ki; + private final byte[] iv; + private final boolean useCounter; + private final int r; + private final byte[] fixedInputData; + + private KDFFeedbackParameters(byte[] ki, byte[] iv, byte[] fixedInputData, int r, boolean useCounter) + { + if (ki == null) + { + throw new IllegalArgumentException("A KDF requires Ki (a seed) as input"); + } + this.ki = Arrays.clone(ki); + + if (fixedInputData == null) + { + this.fixedInputData = new byte[0]; + } + else + { + this.fixedInputData = Arrays.clone(fixedInputData); + } + + this.r = r; + + if (iv == null) + { + this.iv = new byte[0]; + } + else + { + this.iv = Arrays.clone(iv); + } + + this.useCounter = useCounter; + } + + public static KDFFeedbackParameters createWithCounter( + byte[] ki, final byte[] iv, byte[] fixedInputData, int r) + { + if (r != 8 && r != 16 && r != 24 && r != 32) + { + throw new IllegalArgumentException("Length of counter should be 8, 16, 24 or 32"); + } + + return new KDFFeedbackParameters(ki, iv, fixedInputData, r, true); + } + + public static KDFFeedbackParameters createWithoutCounter( + byte[] ki, final byte[] iv, byte[] fixedInputData) + { + return new KDFFeedbackParameters(ki, iv, fixedInputData, UNUSED_R, false); + } + + public byte[] getKI() + { + return ki; + } + + public byte[] getIV() + { + return iv; + } + + public boolean useCounter() + { + return useCounter; + } + + public int getR() + { + return r; + } + + public byte[] getFixedInputData() + { + return Arrays.clone(fixedInputData); + } +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/KDFParameters.java b/core/src/main/java/org/spongycastle/crypto/params/KDFParameters.java new file mode 100644 index 00000000..60010615 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/KDFParameters.java @@ -0,0 +1,31 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/KeyParameter.java b/core/src/main/java/org/spongycastle/crypto/params/KeyParameter.java new file mode 100644 index 00000000..079e136b --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/KeyParameter.java @@ -0,0 +1,30 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/MGFParameters.java b/core/src/main/java/org/spongycastle/crypto/params/MGFParameters.java new file mode 100644 index 00000000..082e37cd --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/MGFParameters.java @@ -0,0 +1,32 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/MQVPrivateParameters.java b/core/src/main/java/org/spongycastle/crypto/params/MQVPrivateParameters.java new file mode 100644 index 00000000..ae917ab8 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/MQVPrivateParameters.java @@ -0,0 +1,43 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/MQVPublicParameters.java b/core/src/main/java/org/spongycastle/crypto/params/MQVPublicParameters.java new file mode 100644 index 00000000..9f6f67de --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/MQVPublicParameters.java @@ -0,0 +1,28 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/NaccacheSternKeyGenerationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/NaccacheSternKeyGenerationParameters.java new file mode 100644 index 00000000..c0b5998b --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/NaccacheSternKeyGenerationParameters.java @@ -0,0 +1,97 @@ +package org.spongycastle.crypto.params; + +import java.security.SecureRandom; + +import org.spongycastle.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/spongycastle/crypto/params/NaccacheSternKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/NaccacheSternKeyParameters.java new file mode 100644 index 00000000..4b855b3f --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/NaccacheSternKeyParameters.java @@ -0,0 +1,53 @@ +package org.spongycastle.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/spongycastle/crypto/params/NaccacheSternPrivateKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/NaccacheSternPrivateKeyParameters.java new file mode 100644 index 00000000..7fa61c94 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/NaccacheSternPrivateKeyParameters.java @@ -0,0 +1,50 @@ +package org.spongycastle.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/spongycastle/crypto/params/ParametersWithIV.java b/core/src/main/java/org/spongycastle/crypto/params/ParametersWithIV.java new file mode 100644 index 00000000..7fb39b7e --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ParametersWithIV.java @@ -0,0 +1,39 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/ParametersWithRandom.java b/core/src/main/java/org/spongycastle/crypto/params/ParametersWithRandom.java new file mode 100644 index 00000000..ca445554 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ParametersWithRandom.java @@ -0,0 +1,36 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/ParametersWithSBox.java b/core/src/main/java/org/spongycastle/crypto/params/ParametersWithSBox.java new file mode 100644 index 00000000..a1046476 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ParametersWithSBox.java @@ -0,0 +1,28 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/ParametersWithSalt.java b/core/src/main/java/org/spongycastle/crypto/params/ParametersWithSalt.java new file mode 100644 index 00000000..e6566a36 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/ParametersWithSalt.java @@ -0,0 +1,42 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/RC2Parameters.java b/core/src/main/java/org/spongycastle/crypto/params/RC2Parameters.java new file mode 100644 index 00000000..ae396f60 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/RC2Parameters.java @@ -0,0 +1,36 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/RC5Parameters.java b/core/src/main/java/org/spongycastle/crypto/params/RC5Parameters.java new file mode 100644 index 00000000..402843e8 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/RC5Parameters.java @@ -0,0 +1,35 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/RSABlindingParameters.java b/core/src/main/java/org/spongycastle/crypto/params/RSABlindingParameters.java new file mode 100644 index 00000000..fd45d205 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/RSABlindingParameters.java @@ -0,0 +1,35 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.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/spongycastle/crypto/params/RSAKeyGenerationParameters.java b/core/src/main/java/org/spongycastle/crypto/params/RSAKeyGenerationParameters.java new file mode 100644 index 00000000..6c047059 --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/RSAKeyGenerationParameters.java @@ -0,0 +1,48 @@ +package org.spongycastle.crypto.params; + +import java.math.BigInteger; +import java.security.SecureRandom; + +import org.spongycastle.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/spongycastle/crypto/params/RSAKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/RSAKeyParameters.java new file mode 100644 index 00000000..d2eceeba --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/RSAKeyParameters.java @@ -0,0 +1,31 @@ +package org.spongycastle.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/spongycastle/crypto/params/RSAPrivateCrtKeyParameters.java b/core/src/main/java/org/spongycastle/crypto/params/RSAPrivateCrtKeyParameters.java new file mode 100644 index 00000000..4dc79bdf --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/RSAPrivateCrtKeyParameters.java @@ -0,0 +1,67 @@ +package org.spongycastle.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; + } +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/SkeinParameters.java b/core/src/main/java/org/spongycastle/crypto/params/SkeinParameters.java new file mode 100644 index 00000000..0f4fc0ef --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/SkeinParameters.java @@ -0,0 +1,329 @@ +package org.spongycastle.crypto.params; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Locale; + +import org.spongycastle.crypto.CipherParameters; +import org.spongycastle.crypto.digests.SkeinDigest; +import org.spongycastle.crypto.digests.SkeinEngine; +import org.spongycastle.crypto.macs.SkeinMac; +import org.spongycastle.util.Integers; + +/** + * Parameters for the Skein hash function - a series of byte[] strings identified by integer tags. + * <p> + * Parameterised Skein can be used for: + * <ul> + * <li>MAC generation, by providing a {@link SkeinParameters.Builder#setKey(byte[]) key}.</li> + * <li>Randomised hashing, by providing a {@link SkeinParameters.Builder#setNonce(byte[]) nonce}.</li> + * <li>A hash function for digital signatures, associating a + * {@link SkeinParameters.Builder#setPublicKey(byte[]) public key} with the message digest.</li> + * <li>A key derivation function, by providing a + * {@link SkeinParameters.Builder#setKeyIdentifier(byte[]) key identifier}.</li> + * <li>Personalised hashing, by providing a + * {@link SkeinParameters.Builder#setPersonalisation(Date, String, String) recommended format} or + * {@link SkeinParameters.Builder#setPersonalisation(byte[]) arbitrary} personalisation string.</li> + * </ul> + * + * @see SkeinEngine + * @see SkeinDigest + * @see SkeinMac + */ +public class SkeinParameters + implements CipherParameters +{ + /** + * The parameter type for a secret key, supporting MAC or KDF functions: {@value + * #PARAM_TYPE_KEY}. + */ + public static final int PARAM_TYPE_KEY = 0; + + /** + * The parameter type for the Skein configuration block: {@value #PARAM_TYPE_CONFIG}. + */ + public static final int PARAM_TYPE_CONFIG = 4; + + /** + * The parameter type for a personalisation string: {@value #PARAM_TYPE_PERSONALISATION}. + */ + public static final int PARAM_TYPE_PERSONALISATION = 8; + + /** + * The parameter type for a public key: {@value #PARAM_TYPE_PUBLIC_KEY}. + */ + public static final int PARAM_TYPE_PUBLIC_KEY = 12; + + /** + * The parameter type for a key identifier string: {@value #PARAM_TYPE_KEY_IDENTIFIER}. + */ + public static final int PARAM_TYPE_KEY_IDENTIFIER = 16; + + /** + * The parameter type for a nonce: {@value #PARAM_TYPE_NONCE}. + */ + public static final int PARAM_TYPE_NONCE = 20; + + /** + * The parameter type for the message: {@value #PARAM_TYPE_MESSAGE}. + */ + public static final int PARAM_TYPE_MESSAGE = 48; + + /** + * The parameter type for the output transformation: {@value #PARAM_TYPE_OUTPUT}. + */ + public static final int PARAM_TYPE_OUTPUT = 63; + + private Hashtable parameters; + + public SkeinParameters() + { + this(new Hashtable()); + } + + private SkeinParameters(final Hashtable parameters) + { + this.parameters = parameters; + } + + /** + * Obtains a map of type (Integer) to value (byte[]) for the parameters tracked in this object. + */ + public Hashtable getParameters() + { + return parameters; + } + + /** + * Obtains the value of the {@link #PARAM_TYPE_KEY key parameter}, or <code>null</code> if not + * set. + */ + public byte[] getKey() + { + return (byte[])parameters.get(Integers.valueOf(PARAM_TYPE_KEY)); + } + + /** + * Obtains the value of the {@link #PARAM_TYPE_PERSONALISATION personalisation parameter}, or + * <code>null</code> if not set. + */ + public byte[] getPersonalisation() + { + return (byte[])parameters.get(Integers.valueOf(PARAM_TYPE_PERSONALISATION)); + } + + /** + * Obtains the value of the {@link #PARAM_TYPE_PUBLIC_KEY public key parameter}, or + * <code>null</code> if not set. + */ + public byte[] getPublicKey() + { + return (byte[])parameters.get(Integers.valueOf(PARAM_TYPE_PUBLIC_KEY)); + } + + /** + * Obtains the value of the {@link #PARAM_TYPE_KEY_IDENTIFIER key identifier parameter}, or + * <code>null</code> if not set. + */ + public byte[] getKeyIdentifier() + { + return (byte[])parameters.get(Integers.valueOf(PARAM_TYPE_KEY_IDENTIFIER)); + } + + /** + * Obtains the value of the {@link #PARAM_TYPE_NONCE nonce parameter}, or <code>null</code> if + * not set. + */ + public byte[] getNonce() + { + return (byte[])parameters.get(Integers.valueOf(PARAM_TYPE_NONCE)); + } + + /** + * A builder for {@link SkeinParameters}. + */ + public static class Builder + { + private Hashtable parameters = new Hashtable(); + + public Builder() + { + } + + public Builder(Hashtable paramsMap) + { + Enumeration keys = paramsMap.keys(); + while (keys.hasMoreElements()) + { + Integer key = (Integer)keys.nextElement(); + parameters.put(key, paramsMap.get(key)); + } + } + + public Builder(SkeinParameters params) + { + Enumeration keys = params.parameters.keys(); + while (keys.hasMoreElements()) + { + Integer key = (Integer)keys.nextElement(); + parameters.put(key, params.parameters.get(key)); + } + } + + /** + * Sets a parameters to apply to the Skein hash function.<br> + * Parameter types must be in the range 0,5..62, and cannot use the value {@value + * SkeinParameters#PARAM_TYPE_MESSAGE} (reserved for message body). + * <p> + * Parameters with type < {@value SkeinParameters#PARAM_TYPE_MESSAGE} are processed before + * the message content, parameters with type > {@value SkeinParameters#PARAM_TYPE_MESSAGE} + * are processed after the message and prior to output. + * + * @param type the type of the parameter, in the range 5..62. + * @param value the byte sequence of the parameter. + * @return + */ + public Builder set(int type, byte[] value) + { + if (value == null) + { + throw new IllegalArgumentException("Parameter value must not be null."); + } + if ((type != PARAM_TYPE_KEY) + && (type <= PARAM_TYPE_CONFIG || type >= PARAM_TYPE_OUTPUT || type == PARAM_TYPE_MESSAGE)) + { + throw new IllegalArgumentException("Parameter types must be in the range 0,5..47,49..62."); + } + if (type == PARAM_TYPE_CONFIG) + { + throw new IllegalArgumentException("Parameter type " + PARAM_TYPE_CONFIG + + " is reserved for internal use."); + } + this.parameters.put(Integers.valueOf(type), value); + return this; + } + + /** + * Sets the {@link SkeinParameters#PARAM_TYPE_KEY} parameter. + */ + public Builder setKey(byte[] key) + { + return set(PARAM_TYPE_KEY, key); + } + + /** + * Sets the {@link SkeinParameters#PARAM_TYPE_PERSONALISATION} parameter. + */ + public Builder setPersonalisation(byte[] personalisation) + { + return set(PARAM_TYPE_PERSONALISATION, personalisation); + } + + /** + * Implements the recommended personalisation format for Skein defined in Section 4.11 of + * the Skein 1.3 specification. + * <p> + * The format is <code>YYYYMMDD email@address distinguisher</code>, encoded to a byte + * sequence using UTF-8 encoding. + * + * @param date the date the personalised application of the Skein was defined. + * @param emailAddress the email address of the creation of the personalised application. + * @param distinguisher an arbitrary personalisation string distinguishing the application. + * @return the current builder. + */ + public Builder setPersonalisation(Date date, String emailAddress, String distinguisher) + { + try + { + final ByteArrayOutputStream bout = new ByteArrayOutputStream(); + final OutputStreamWriter out = new OutputStreamWriter(bout, "UTF-8"); + final DateFormat format = new SimpleDateFormat("YYYYMMDD"); + out.write(format.format(date)); + out.write(" "); + out.write(emailAddress); + out.write(" "); + out.write(distinguisher); + out.close(); + return set(PARAM_TYPE_PERSONALISATION, bout.toByteArray()); + } + catch (IOException e) + { + throw new IllegalStateException("Byte I/O failed: " + e); + } + } + + /** + * Implements the recommended personalisation format for Skein defined in Section 4.11 of + * the Skein 1.3 specification. You may need to use this method if the default locale + * doesn't use a Gregorian calender so that the GeneralizedTime produced is compatible implementations. + * <p> + * The format is <code>YYYYMMDD email@address distinguisher</code>, encoded to a byte + * sequence using UTF-8 encoding. + * + * @param date the date the personalised application of the Skein was defined. + * @param dateLocale locale to be used for date interpretation. + * @param emailAddress the email address of the creation of the personalised application. + * @param distinguisher an arbitrary personalisation string distinguishing the application. + * @return the current builder. + */ + public Builder setPersonalisation(Date date, Locale dateLocale, String emailAddress, String distinguisher) + { + try + { + final ByteArrayOutputStream bout = new ByteArrayOutputStream(); + final OutputStreamWriter out = new OutputStreamWriter(bout, "UTF-8"); + final DateFormat format = new SimpleDateFormat("YYYYMMDD", dateLocale); + out.write(format.format(date)); + out.write(" "); + out.write(emailAddress); + out.write(" "); + out.write(distinguisher); + out.close(); + return set(PARAM_TYPE_PERSONALISATION, bout.toByteArray()); + } + catch (IOException e) + { + throw new IllegalStateException("Byte I/O failed: " + e); + } + } + + /** + * Sets the {@link SkeinParameters#PARAM_TYPE_KEY_IDENTIFIER} parameter. + */ + public Builder setPublicKey(byte[] publicKey) + { + return set(PARAM_TYPE_PUBLIC_KEY, publicKey); + } + + /** + * Sets the {@link SkeinParameters#PARAM_TYPE_KEY_IDENTIFIER} parameter. + */ + public Builder setKeyIdentifier(byte[] keyIdentifier) + { + return set(PARAM_TYPE_KEY_IDENTIFIER, keyIdentifier); + } + + /** + * Sets the {@link SkeinParameters#PARAM_TYPE_NONCE} parameter. + */ + public Builder setNonce(byte[] nonce) + { + return set(PARAM_TYPE_NONCE, nonce); + } + + /** + * Constructs a new {@link SkeinParameters} instance with the parameters provided to this + * builder. + */ + public SkeinParameters build() + { + return new SkeinParameters(parameters); + } + } +} diff --git a/core/src/main/java/org/spongycastle/crypto/params/TweakableBlockCipherParameters.java b/core/src/main/java/org/spongycastle/crypto/params/TweakableBlockCipherParameters.java new file mode 100644 index 00000000..2a12186e --- /dev/null +++ b/core/src/main/java/org/spongycastle/crypto/params/TweakableBlockCipherParameters.java @@ -0,0 +1,40 @@ +package org.spongycastle.crypto.params; + +import org.spongycastle.crypto.CipherParameters; +import org.spongycastle.util.Arrays; + +/** + * Parameters for tweakable block ciphers. + */ +public class TweakableBlockCipherParameters + implements CipherParameters +{ + private final byte[] tweak; + private final KeyParameter key; + + public TweakableBlockCipherParameters(final KeyParameter key, final byte[] tweak) + { + this.key = key; + this.tweak = Arrays.clone(tweak); + } + + /** + * Gets the key. + * + * @return the key to use, or <code>null</code> to use the current key. + */ + public KeyParameter getKey() + { + return key; + } + + /** + * Gets the tweak value. + * + * @return the tweak to use, or <code>null</code> to use the current tweak. + */ + public byte[] getTweak() + { + return tweak; + } +} |