Add spongy castle sources to libraries folder
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.spec.KeySpec;
|
||||
|
||||
/**
|
||||
* This class specifies a DES key.
|
||||
*/
|
||||
public class DESKeySpec
|
||||
implements KeySpec
|
||||
{
|
||||
public static final int DES_KEY_LEN = 8;
|
||||
|
||||
private byte[] keyBytes = new byte[DES_KEY_LEN];
|
||||
|
||||
/**
|
||||
* Uses the first 8 bytes in <code>key</code> as the key material for the DES key.
|
||||
* <p>
|
||||
* The bytes that constitute the DES key are those between
|
||||
* <code>key[0]</code> and <code>key[7]</code> inclusive.
|
||||
*
|
||||
* @param key - the buffer with the DES key material.
|
||||
* @exception InvalidKeyException - if the given key material is shorter than 8 bytes.
|
||||
*/
|
||||
public DESKeySpec(
|
||||
byte[] key)
|
||||
throws InvalidKeyException
|
||||
{
|
||||
if (key.length < DES_KEY_LEN)
|
||||
{
|
||||
throw new InvalidKeyException("DES key material too short in construction");
|
||||
}
|
||||
|
||||
System.arraycopy(key, 0, keyBytes, 0, keyBytes.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the first 8 bytes in <code>key</code>, beginning at
|
||||
* <code>offset</code> inclusive, as the key material for the DES key.
|
||||
* <p>
|
||||
* The bytes that constitute the DES key are those between
|
||||
* <code>key[offset]</code> and <code>key[offset+7]</code> inclusive.
|
||||
*
|
||||
* @param key the buffer with the DES key material.
|
||||
* @param offset the offset in <code>key</code>, where the DES key material starts.
|
||||
* @exception InvalidKeyException if the given key material, starting at
|
||||
* <code>offset</code> inclusive, is shorter than 8 bytes.
|
||||
*/
|
||||
public DESKeySpec(
|
||||
byte[] key,
|
||||
int offset)
|
||||
throws InvalidKeyException
|
||||
{
|
||||
if ((key.length - offset) < DES_KEY_LEN)
|
||||
{
|
||||
throw new InvalidKeyException("DES key material too short in construction");
|
||||
}
|
||||
|
||||
System.arraycopy(key, offset, keyBytes, 0, keyBytes.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the DES key material.
|
||||
*
|
||||
* @return the DES key material.
|
||||
*/
|
||||
public byte[] getKey()
|
||||
{
|
||||
byte[] tmp = new byte[DES_KEY_LEN];
|
||||
|
||||
System.arraycopy(keyBytes, 0, tmp, 0, tmp.length);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given DES key material, starting at <code>offset</code>
|
||||
* inclusive, is parity-adjusted.
|
||||
*
|
||||
* @param key the buffer with the DES key material.
|
||||
* @param offset the offset in <code>key</code>, where the DES key material starts.
|
||||
* @returns true if the given DES key material is parity-adjusted, false otherwise.
|
||||
* @exception InvalidKeyException if the given key material, starting at <code>offset</code>
|
||||
* inclusive, is shorter than 8 bytes.
|
||||
*/
|
||||
public static boolean isParityAdjusted(
|
||||
byte[] key,
|
||||
int offset)
|
||||
throws InvalidKeyException
|
||||
{
|
||||
if ((key.length - offset) < DES_KEY_LEN)
|
||||
{
|
||||
throw new InvalidKeyException("key material too short in DESKeySpec.isParityAdjusted");
|
||||
}
|
||||
|
||||
for (int i = 0; i < DES_KEY_LEN; i++)
|
||||
{
|
||||
byte keyByte = key[i + offset];
|
||||
int count = 0;
|
||||
|
||||
keyByte = (byte)((keyByte & 0xff) >> 1);
|
||||
|
||||
while (keyByte != 0)
|
||||
{
|
||||
/*
|
||||
* we increment for every "on" bit
|
||||
*/
|
||||
if ((keyByte & 0x01) != 0)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
keyByte = (byte)((keyByte & 0xff) >> 1);
|
||||
}
|
||||
|
||||
if ((count & 1) == 1)
|
||||
{
|
||||
if ((key[i + offset] & 1) == 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if ((key[i + offset] & 1) != 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if the given DES key material is weak or semi-weak.
|
||||
*
|
||||
* @param key the buffer with the DES key material.
|
||||
* @param offset the offset in <code>key</code>, where the DES key
|
||||
* material starts.
|
||||
* @return true if the given DES key material is weak or semi-weak, false otherwise.
|
||||
* @exception InvalidKeyException if the given key material, starting at <code>offset</code>
|
||||
* inclusive, is shorter than 8 bytes.
|
||||
*/
|
||||
public static boolean isWeak(
|
||||
byte[] key,
|
||||
int offset)
|
||||
throws InvalidKeyException
|
||||
{
|
||||
if (key.length - offset < DES_KEY_LEN)
|
||||
{
|
||||
throw new InvalidKeyException("key material too short in DESKeySpec.isWeak");
|
||||
}
|
||||
|
||||
nextkey: for (int i = 0; i < N_DES_WEAK_KEYS; i++)
|
||||
{
|
||||
for (int j = 0; j < DES_KEY_LEN; j++)
|
||||
{
|
||||
if (key[j + offset] != DES_weak_keys[i * DES_KEY_LEN + j])
|
||||
{
|
||||
continue nextkey;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.spec.KeySpec;
|
||||
|
||||
/**
|
||||
* This class specifies a DES-EDE ("triple-DES") key.
|
||||
*/
|
||||
public class DESedeKeySpec
|
||||
implements KeySpec
|
||||
{
|
||||
public static final int DES_EDE_KEY_LEN = 24;
|
||||
|
||||
private byte[] keyBytes = new byte[DES_EDE_KEY_LEN];
|
||||
|
||||
/**
|
||||
* Uses the first 24 bytes in <code>key</code> as the DES-EDE key.
|
||||
* <p>
|
||||
* The bytes that constitute the DES-EDE key are those between
|
||||
* <code>key[0]</code> and <code>key[23]</code> inclusive
|
||||
*
|
||||
* @param key the buffer with the DES-EDE key material.
|
||||
* @exception InvalidKeyException if the given key material is shorter
|
||||
* than 24 bytes.
|
||||
*/
|
||||
public DESedeKeySpec(
|
||||
byte[] key)
|
||||
throws InvalidKeyException
|
||||
{
|
||||
if (key.length < DES_EDE_KEY_LEN)
|
||||
{
|
||||
throw new InvalidKeyException("DESede key material too short in construction");
|
||||
}
|
||||
|
||||
System.arraycopy(key, 0, keyBytes, 0, keyBytes.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the first 24 bytes in <code>key</code>, beginning at
|
||||
* <code>offset</code> inclusive, as the DES-EDE key.
|
||||
* <p>
|
||||
* The bytes that constitute the DES-EDE key are those between
|
||||
* <code>key[offset]</code> and <code>key[offset+23]</code> inclusive.
|
||||
* @param key the buffer with the DES-EDE key material.
|
||||
* @param offset the offset in <code>key</code>, where the DES-EDE key
|
||||
* material starts.
|
||||
* @exception InvalidKeyException if the given key material, starting at
|
||||
* <code>offset</code> inclusive, is shorter than 24 bytes
|
||||
*/
|
||||
public DESedeKeySpec(
|
||||
byte[] key,
|
||||
int offset)
|
||||
throws InvalidKeyException
|
||||
{
|
||||
if ((key.length - offset) < DES_EDE_KEY_LEN)
|
||||
{
|
||||
throw new InvalidKeyException("DESede key material too short in construction");
|
||||
}
|
||||
|
||||
System.arraycopy(key, 0, keyBytes, 0, keyBytes.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the DES-EDE key.
|
||||
*
|
||||
* @return the DES-EDE key
|
||||
*/
|
||||
public byte[] getKey()
|
||||
{
|
||||
byte[] tmp = new byte[DES_EDE_KEY_LEN];
|
||||
|
||||
System.arraycopy(keyBytes, 0, tmp, 0, tmp.length);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given DES-EDE key, starting at <code>offset</code>
|
||||
* inclusive, is parity-adjusted.
|
||||
*
|
||||
* @return true if the given DES-EDE key is parity-adjusted, false
|
||||
* otherwise
|
||||
* @exception InvalidKeyException if the given key material, starting at
|
||||
* <code>offset</code> inclusive, is shorter than 24 bytes
|
||||
*/
|
||||
public static boolean isParityAdjusted(
|
||||
byte[] key,
|
||||
int offset)
|
||||
throws InvalidKeyException
|
||||
{
|
||||
if ((key.length - offset) < DES_EDE_KEY_LEN)
|
||||
{
|
||||
throw new InvalidKeyException("key material too short in DESedeKeySpec.isParityAdjusted");
|
||||
}
|
||||
|
||||
return (DESKeySpec.isParityAdjusted(key, offset)
|
||||
&& DESKeySpec.isParityAdjusted(key, offset + 8)
|
||||
&& DESKeySpec.isParityAdjusted(key, offset + 16));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
/**
|
||||
* This class specifies the set of parameters used for generating
|
||||
* Diffie-Hellman (system) parameters for use in Diffie-Hellman key
|
||||
* agreement. This is typically done by a central
|
||||
* authority.
|
||||
* <p>
|
||||
* The central authority, after computing the parameters, must send this
|
||||
* information to the parties looking to agree on a secret key.
|
||||
*/
|
||||
public class DHGenParameterSpec
|
||||
implements AlgorithmParameterSpec
|
||||
{
|
||||
private int primeSize;
|
||||
private int exponentSize;
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for the generation of Diffie-Hellman
|
||||
* (system) parameters. The constructed parameter set can be used to
|
||||
* initialize an <a href="http://java.sun.com/products/jdk/1.2/docs/api/java.security.AlgorithmParameterGenerator.html"><code>AlgorithmParameterGenerator</code></a>
|
||||
* object for the generation of Diffie-Hellman parameters.
|
||||
*
|
||||
* @param primeSize the size (in bits) of the prime modulus.
|
||||
* @param exponentSize the size (in bits) of the random exponent.
|
||||
*/
|
||||
public DHGenParameterSpec(
|
||||
int primeSize,
|
||||
int exponentSize)
|
||||
{
|
||||
this.primeSize = primeSize;
|
||||
this.exponentSize = exponentSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size in bits of the prime modulus.
|
||||
*
|
||||
* @return the size in bits of the prime modulus
|
||||
*/
|
||||
public int getPrimeSize()
|
||||
{
|
||||
return primeSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size in bits of the random exponent (private value).
|
||||
*
|
||||
* @return the size in bits of the random exponent (private value)
|
||||
*/
|
||||
public int getExponentSize()
|
||||
{
|
||||
return exponentSize;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
/**
|
||||
* This class specifies the set of parameters used with the Diffie-Hellman
|
||||
* algorithm, as specified in PKCS #3: <i>Diffie-Hellman Key-Agreement
|
||||
* Standard</i>.
|
||||
* <p>
|
||||
* A central authority generates parameters and gives them to the two
|
||||
* entities seeking to generate a secret key. The parameters are a prime
|
||||
* <code>p</code>, a base <code>g</code>, and optionally the length
|
||||
* in bits of the private value, <code>l</code>.
|
||||
* <p>
|
||||
* It is possible that more than one instance of parameters may be
|
||||
* generated by a given central authority, and that there may be more than
|
||||
* one central authority. Indeed, each individual may be its own central
|
||||
* authority, with different entities having different parameters.
|
||||
*
|
||||
* @see javax.crypto.KeyAgreement
|
||||
*/
|
||||
public class DHParameterSpec
|
||||
implements AlgorithmParameterSpec
|
||||
{
|
||||
private BigInteger p;
|
||||
private BigInteger g;
|
||||
private int l;
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for Diffie-Hellman, using a prime modulus
|
||||
* <code>p</code> and a base generator <code>g</code>.
|
||||
*
|
||||
* @param p the prime modulus
|
||||
* @param g the base generator
|
||||
*/
|
||||
public DHParameterSpec(
|
||||
BigInteger p,
|
||||
BigInteger g)
|
||||
{
|
||||
this.p = p;
|
||||
this.g = g;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for Diffie-Hellman, using a prime modulus
|
||||
* <code>p</code>, a base generator <code>g</code>, and the size in bits,
|
||||
* <code>l</code>, of the random exponent (private value).
|
||||
*
|
||||
* @param p the prime modulus
|
||||
* @param g the base generator
|
||||
* @param l the size in bits of the random exponent (private value)
|
||||
*/
|
||||
public DHParameterSpec(
|
||||
BigInteger p,
|
||||
BigInteger g,
|
||||
int l)
|
||||
{
|
||||
this.p = p;
|
||||
this.g = g;
|
||||
this.l = l;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the prime modulus <code>p</code>.
|
||||
*
|
||||
* @return the prime modulus <code>p</code>
|
||||
*/
|
||||
public BigInteger getP()
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the base generator <code>g</code>.
|
||||
*
|
||||
* @return the base generator <code>g</code>
|
||||
*/
|
||||
public BigInteger getG()
|
||||
{
|
||||
return g;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size in bits, <code>l</code>, of the random exponent
|
||||
* (private value).
|
||||
*
|
||||
* @return the size in bits, <code>l</code>, of the random exponent
|
||||
* (private value), or 0 if this size has not been set
|
||||
*/
|
||||
public int getL()
|
||||
{
|
||||
return l;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.spec.KeySpec;
|
||||
|
||||
/**
|
||||
* This class specifies a Diffie-Hellman private key with its associated parameters.
|
||||
*
|
||||
* @see DHPublicKeySpec
|
||||
*/
|
||||
public class DHPrivateKeySpec
|
||||
implements KeySpec
|
||||
{
|
||||
private BigInteger x;
|
||||
private BigInteger p;
|
||||
private BigInteger g;
|
||||
|
||||
/**
|
||||
* Constructor that takes a private value <code>x</code>, a prime
|
||||
* modulus <code>p</code>, and a base generator <code>g</code>.
|
||||
*/
|
||||
public DHPrivateKeySpec(
|
||||
BigInteger x,
|
||||
BigInteger p,
|
||||
BigInteger g)
|
||||
{
|
||||
this.x = x;
|
||||
this.p = p;
|
||||
this.g = g;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the private value <code>x</code>.
|
||||
*
|
||||
* @return the private value <code>x</code>
|
||||
*/
|
||||
public BigInteger getX()
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the prime modulus <code>p</code>.
|
||||
*
|
||||
* @return the prime modulus <code>p</code>
|
||||
*/
|
||||
public BigInteger getP()
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the base generator <code>g</code>.
|
||||
*
|
||||
* @return the base generator <code>g</code>
|
||||
*/
|
||||
public BigInteger getG()
|
||||
{
|
||||
return g;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.spec.KeySpec;
|
||||
|
||||
/**
|
||||
* This class specifies a Diffie-Hellman public key with its associated parameters.
|
||||
*
|
||||
* @see DHPrivateKeySpec
|
||||
*/
|
||||
public class DHPublicKeySpec
|
||||
implements KeySpec
|
||||
{
|
||||
private BigInteger y;
|
||||
private BigInteger p;
|
||||
private BigInteger g;
|
||||
|
||||
/**
|
||||
* Constructor that takes a public value <code>y</code>, a prime
|
||||
* modulus <code>p</code>, and a base generator <code>g</code>.
|
||||
*/
|
||||
public DHPublicKeySpec(
|
||||
BigInteger y,
|
||||
BigInteger p,
|
||||
BigInteger g)
|
||||
{
|
||||
this.y = y;
|
||||
this.p = p;
|
||||
this.g = g;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the public value <code>y</code>.
|
||||
*
|
||||
* @return the public value <code>y</code>
|
||||
*/
|
||||
public BigInteger getY()
|
||||
{
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the prime modulus <code>p</code>.
|
||||
*
|
||||
* @return the prime modulus <code>p</code>
|
||||
*/
|
||||
public BigInteger getP()
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the base generator <code>g</code>.
|
||||
*
|
||||
* @return the base generator <code>g</code>
|
||||
*/
|
||||
public BigInteger getG()
|
||||
{
|
||||
return g;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
/**
|
||||
* This class specifies an <i>initialization vector</i> (IV). IVs are used
|
||||
* by ciphers in feedback mode, e.g., DES in CBC mode.
|
||||
*/
|
||||
public class IvParameterSpec
|
||||
implements AlgorithmParameterSpec
|
||||
{
|
||||
private byte[] iv;
|
||||
|
||||
/**
|
||||
* Uses the bytes in <code>iv</code> as the IV.
|
||||
*
|
||||
* @param iv the buffer with the IV
|
||||
*/
|
||||
public IvParameterSpec(
|
||||
byte[] iv)
|
||||
{
|
||||
if (iv == null)
|
||||
{
|
||||
throw new IllegalArgumentException("null iv passed");
|
||||
}
|
||||
|
||||
this.iv = new byte[iv.length];
|
||||
|
||||
System.arraycopy(iv, 0, this.iv, 0, iv.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the first <code>len</code> bytes in <code>iv</code>,
|
||||
* beginning at <code>offset</code> inclusive, as the IV.
|
||||
* <p>
|
||||
* The bytes that constitute the IV are those between
|
||||
* <code>iv[offset]</code> and <code>iv[offset+len-1]</code> inclusive.
|
||||
*
|
||||
* @param iv the buffer with the IV
|
||||
* @param offset the offset in <code>iv</code> where the IV starts
|
||||
* @param len the number of IV bytes
|
||||
*/
|
||||
public IvParameterSpec(
|
||||
byte[] iv,
|
||||
int offset,
|
||||
int len)
|
||||
{
|
||||
if (iv == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Null iv passed");
|
||||
}
|
||||
|
||||
if (offset < 0 || len < 0 || (iv.length - offset) < len)
|
||||
{
|
||||
throw new IllegalArgumentException("Bad offset/len");
|
||||
}
|
||||
|
||||
this.iv = new byte[len];
|
||||
|
||||
System.arraycopy(iv, offset, this.iv, 0, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the initialization vector (IV).
|
||||
*
|
||||
* @return the initialization vector (IV)
|
||||
*/
|
||||
public byte[] getIV()
|
||||
{
|
||||
byte[] tmp = new byte[iv.length];
|
||||
|
||||
System.arraycopy(iv, 0, tmp, 0, iv.length);
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
/**
|
||||
* This class specifies the set of parameters used with OAEP Padding, as defined
|
||||
* in the PKCS #1 standard. Its ASN.1 definition in PKCS#1 standard is described
|
||||
* below:
|
||||
*
|
||||
* </pre>
|
||||
*
|
||||
* RSAES-OAEP-params ::= SEQUENCE { hashAlgorithm [0] OAEP-PSSDigestAlgorithms
|
||||
* DEFAULT sha1, maskGenAlgorithm [1] PKCS1MGFAlgorithms DEFAULT mgf1SHA1,
|
||||
* pSourceAlgorithm [2] PKCS1PSourceAlgorithms DEFAULT pSpecifiedEmpty }
|
||||
*
|
||||
* </pre>
|
||||
*
|
||||
* where
|
||||
*
|
||||
* <pre>
|
||||
*
|
||||
* OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= { { OID id-sha1 PARAMETERS
|
||||
* NULL }| { OID id-sha256 PARAMETERS NULL }| { OID id-sha384 PARAMETERS NULL } | {
|
||||
* OID id-sha512 PARAMETERS NULL }, ... -- Allows for future expansion -- }
|
||||
* PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= { { OID id-mgf1 PARAMETERS
|
||||
* OAEP-PSSDigestAlgorithms }, ... -- Allows for future expansion -- }
|
||||
* PKCS1PSourceAlgorithms ALGORITHM-IDENTIFIER ::= { { OID id-pSpecified
|
||||
* PARAMETERS OCTET STRING }, ... -- Allows for future expansion -- }
|
||||
*
|
||||
* </pre>
|
||||
*
|
||||
* @see PSource
|
||||
*/
|
||||
public class OAEPParameterSpec
|
||||
implements AlgorithmParameterSpec
|
||||
{
|
||||
private String mdName;
|
||||
private String mgfName;
|
||||
private AlgorithmParameterSpec mgfSpec;
|
||||
private PSource pSrc;
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for OAEP padding as defined in the PKCS #1
|
||||
* standard using the specified message digest algorithm mdName, mask
|
||||
* generation function algorithm mgfName, parameters for the mask generation
|
||||
* function mgfSpec, and source of the encoding input P pSrc.
|
||||
*
|
||||
* @param mdName the algorithm name for the message digest.
|
||||
* @param mgfName the algorithm name for the mask generation function.
|
||||
* @param mgfSpec the parameters for the mask generation function. If null is
|
||||
* specified, null will be returned by getMGFParameters().
|
||||
* @param pSrc the source of the encoding input P.
|
||||
* @throws NullPointerException if mdName, mgfName, or pSrc is null.
|
||||
*/
|
||||
public OAEPParameterSpec(String mdName, String mgfName,
|
||||
AlgorithmParameterSpec mgfSpec, PSource pSrc)
|
||||
{
|
||||
this.mdName = mdName;
|
||||
this.mgfName = mgfName;
|
||||
this.mgfSpec = mgfSpec;
|
||||
this.pSrc = pSrc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message digest algorithm name.
|
||||
*
|
||||
* @return the message digest algorithm name.
|
||||
*/
|
||||
public String getDigestAlgorithm()
|
||||
{
|
||||
return mdName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mask generation function algorithm name.
|
||||
*
|
||||
* @return the mask generation function algorithm name.
|
||||
*/
|
||||
public String getMGFAlgorithm()
|
||||
{
|
||||
return mgfName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parameters for the mask generation function.
|
||||
*
|
||||
* @return the parameters for the mask generation function.
|
||||
*/
|
||||
public AlgorithmParameterSpec getMGFParameters()
|
||||
{
|
||||
return mgfSpec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the source of encoding input P.
|
||||
*
|
||||
* @return the source of encoding input P.
|
||||
*/
|
||||
public PSource getPSource()
|
||||
{
|
||||
return pSrc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.security.spec.KeySpec;
|
||||
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
|
||||
/**
|
||||
* A user-chosen password that can be used with password-based encryption (PBE).
|
||||
* <p>
|
||||
* The password can be viewed as some kind of raw key material, from which the
|
||||
* encryption mechanism that uses it derives a cryptographic key.
|
||||
* <p>
|
||||
* Different PBE mechanisms may consume different bits of each password
|
||||
* character. For example, the PBE mechanism defined in PKCS #5 looks at only
|
||||
* the low order 8 bits of each character, whereas PKCS #12 looks at all 16 bits
|
||||
* of each character.
|
||||
* <p>
|
||||
* You convert the password characters to a PBE key by creating an instance of
|
||||
* the appropriate secret-key factory. For example, a secret-key factory for
|
||||
* PKCS #5 will construct a PBE key from only the low order 8 bits of each
|
||||
* password character, whereas a secret-key factory for PKCS #12 will take all
|
||||
* 16 bits of each character.
|
||||
* <p>
|
||||
* Also note that this class stores passwords as char arrays instead of String
|
||||
* objects (which would seem more logical), because the String class is
|
||||
* immutable and there is no way to overwrite its internal value when the
|
||||
* password stored in it is no longer needed. Hence, this class requests the
|
||||
* password as a char array, so it can be overwritten when done.
|
||||
*
|
||||
* @see SecretKeyFactory
|
||||
* @see PBEParameterSpec
|
||||
*/
|
||||
public class PBEKeySpec
|
||||
implements KeySpec
|
||||
{
|
||||
|
||||
private char[] password;
|
||||
|
||||
private byte[] salt;
|
||||
|
||||
private int iterationCount;
|
||||
|
||||
private int keyLength;
|
||||
|
||||
private boolean isPasswordCleared;
|
||||
|
||||
/**
|
||||
* Constructor that takes a password. An empty char[] is used if null is
|
||||
* specified.
|
||||
* <p>
|
||||
* Note: password is cloned before it is stored in the new PBEKeySpec
|
||||
* object.
|
||||
*
|
||||
* @param password -
|
||||
* the password.
|
||||
*/
|
||||
public PBEKeySpec(char[] password)
|
||||
{
|
||||
if (password == null)
|
||||
{
|
||||
this.password = new char[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
this.password = new char[password.length];
|
||||
|
||||
System.arraycopy(password, 0, this.password, 0, password.length);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of the password.
|
||||
* <p>
|
||||
* Note: this method returns a copy of the password. It is the caller's
|
||||
* responsibility to zero out the password information after it is no longer
|
||||
* needed.
|
||||
*
|
||||
* @return the password
|
||||
* @throws IllegalStateException -
|
||||
* if password has been cleared by calling clearPassword method.
|
||||
*/
|
||||
public final char[] getPassword()
|
||||
{
|
||||
if (isPasswordCleared)
|
||||
{
|
||||
throw new IllegalStateException("Password has been cleared");
|
||||
}
|
||||
return password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that takes a password, salt, iteration count, and
|
||||
* to-be-derived key length for generating PBEKey of variable-key-size PBE
|
||||
* ciphers. An empty char[] is used if null is specified for password.
|
||||
* <p>
|
||||
* Note: the password and salt are cloned before they are stored in the new
|
||||
* PBEKeySpec object.
|
||||
*
|
||||
*
|
||||
* @param password
|
||||
* password - the password.
|
||||
* @param salt
|
||||
* salt - the salt.
|
||||
* @param iterationCount
|
||||
* iterationCount - the iteration count.
|
||||
* @param keyLength
|
||||
* keyLength - the to-be-derived key length.
|
||||
* @throws NullPointerException -
|
||||
* if salt is null.
|
||||
* @throws IllegalArgumentException -
|
||||
* if salt is empty, i.e. 0-length, iterationCount or keyLength
|
||||
* is not positive.
|
||||
*/
|
||||
public PBEKeySpec(char[] password, byte[] salt, int iterationCount, int keyLength)
|
||||
{
|
||||
this(password);
|
||||
if (salt == null)
|
||||
{
|
||||
throw new NullPointerException("salt is null");
|
||||
}
|
||||
if (salt.length == 0)
|
||||
{
|
||||
throw new IllegalArgumentException("salt is empty");
|
||||
}
|
||||
if (iterationCount < 0)
|
||||
{
|
||||
throw new IllegalArgumentException("iterationCount is not positive");
|
||||
}
|
||||
if (keyLength < 0)
|
||||
{
|
||||
throw new IllegalArgumentException("keyLength is not positive");
|
||||
}
|
||||
this.keyLength = keyLength;
|
||||
this.iterationCount = iterationCount;
|
||||
this.salt = (byte[]) salt.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that takes a password, salt, iteration count for generating
|
||||
* PBEKey of fixed-key-size PBE ciphers. An empty char[] is used if null is
|
||||
* specified for password.
|
||||
* <p>
|
||||
* Note: the password and salt are cloned before they are stored in the new
|
||||
* PBEKeySpec object.
|
||||
*
|
||||
* @param password -
|
||||
* the password.
|
||||
* @param salt -
|
||||
* the salt.
|
||||
* @param iterationCount -
|
||||
* the iteration count.
|
||||
* @throws NullPointerException -
|
||||
* if salt is null.
|
||||
* @throws IllegalArgumentException -
|
||||
* if salt is empty, i.e. 0-length, or iterationCount is not
|
||||
* positive.
|
||||
*/
|
||||
public PBEKeySpec(char[] password, byte[] salt, int iterationCount)
|
||||
{
|
||||
this(password, salt, iterationCount, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the internal copy of the password.
|
||||
*/
|
||||
public final void clearPassword()
|
||||
{
|
||||
for (int i = 0; i < password.length; i++)
|
||||
{
|
||||
password[i] = 0;
|
||||
}
|
||||
password = null;
|
||||
isPasswordCleared = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of the salt or null if not specified.
|
||||
*
|
||||
* Note: this method should return a copy of the salt. It is the caller's
|
||||
* responsibility to zero out the salt information after it is no longer
|
||||
* needed.
|
||||
*
|
||||
* @return the salt.
|
||||
*/
|
||||
public final byte[] getSalt()
|
||||
{
|
||||
if (salt != null)
|
||||
{
|
||||
byte[] tmp = new byte[salt.length];
|
||||
|
||||
System.arraycopy(salt, 0, tmp, 0, salt.length);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the iteration count or 0 if not specified.
|
||||
*
|
||||
* @return the iteration count.
|
||||
*/
|
||||
public final int getIterationCount()
|
||||
{
|
||||
return iterationCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the to-be-derived key length or 0 if not specified.
|
||||
* <p>
|
||||
* Note: this is used to indicate the preference on key length for
|
||||
* variable-key-size ciphers. The actual key size depends on each provider's
|
||||
* implementation.
|
||||
*
|
||||
* @return the to-be-derived key length.
|
||||
*/
|
||||
public final int getKeyLength()
|
||||
{
|
||||
return keyLength;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
/**
|
||||
* This class specifies the set of parameters used with password-based encryption (PBE), as defined in the
|
||||
* <a href="http://www.rsa.com/rsalabs/pubs/PKCS/html/pkcs-5.html">PKCS #5</a> standard.
|
||||
*/
|
||||
public class PBEParameterSpec
|
||||
implements AlgorithmParameterSpec
|
||||
{
|
||||
private byte[] salt;
|
||||
private int iterationCount;
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for password-based encryption as defined in
|
||||
* the PKCS #5 standard.
|
||||
*
|
||||
* @param salt the salt.
|
||||
* @param iterationCount the iteration count.
|
||||
*/
|
||||
public PBEParameterSpec(
|
||||
byte[] salt,
|
||||
int iterationCount)
|
||||
{
|
||||
this.salt = new byte[salt.length];
|
||||
System.arraycopy(salt, 0, this.salt, 0, salt.length);
|
||||
|
||||
this.iterationCount = iterationCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the salt.
|
||||
*
|
||||
* @return the salt
|
||||
*/
|
||||
public byte[] getSalt()
|
||||
{
|
||||
byte[] tmp = new byte[salt.length];
|
||||
|
||||
System.arraycopy(salt, 0, tmp, 0, salt.length);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the iteration count.
|
||||
*
|
||||
* @return the iteration count
|
||||
*/
|
||||
public int getIterationCount()
|
||||
{
|
||||
return iterationCount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
/**
|
||||
* This class specifies the source for encoding input P in OAEP Padding, as
|
||||
* defined in the {@link http://www.ietf.org/rfc/rfc3447.txt PKCS #1} standard.
|
||||
*
|
||||
* <pre>
|
||||
*
|
||||
* PKCS1PSourceAlgorithms ALGORITHM-IDENTIFIER ::= {
|
||||
* { OID id-pSpecified PARAMETERS OCTET STRING },
|
||||
* ... -- Allows for future expansion --
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public class PSource
|
||||
{
|
||||
/**
|
||||
* This class is used to explicitly specify the value for encoding input P
|
||||
* in OAEP Padding.
|
||||
*
|
||||
*/
|
||||
public final static class PSpecified
|
||||
extends PSource
|
||||
{
|
||||
private byte[] p;
|
||||
|
||||
/**
|
||||
* The encoding input P whose value equals byte[0].
|
||||
*/
|
||||
public static final PSpecified DEFAULT = new PSpecified(new byte[0]);
|
||||
|
||||
/**
|
||||
* Constructs the source explicitly with the specified value p as the
|
||||
* encoding input P.
|
||||
*
|
||||
* @param p the value of the encoding input. The contents of the array
|
||||
* are copied to protect against subsequent modification.
|
||||
* @throws NullPointerException if p is null.
|
||||
*/
|
||||
public PSpecified(byte[] p)
|
||||
{
|
||||
super("PSpecified");
|
||||
if (p == null)
|
||||
{
|
||||
throw new NullPointerException("The encoding input is null");
|
||||
}
|
||||
this.p = copyOf(p);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of encoding input P.
|
||||
*
|
||||
* @return the value of encoding input P. A new array is returned each
|
||||
* time this method is called.
|
||||
*/
|
||||
public byte[] getValue()
|
||||
{
|
||||
return copyOf(p);
|
||||
}
|
||||
|
||||
private byte[] copyOf(byte[] b)
|
||||
{
|
||||
byte[] tmp = new byte[b.length];
|
||||
|
||||
System.arraycopy(b, 0, tmp, 0, b.length);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
||||
private String pSrcName;
|
||||
|
||||
/**
|
||||
* Constructs a source of the encoding input P for OAEP padding as defined
|
||||
* in the PKCS #1 standard using the specified PSource algorithm.
|
||||
*
|
||||
* @param pSrcName the algorithm for the source of the encoding input P.
|
||||
* @throws NullPointerException if pSrcName is null.
|
||||
*/
|
||||
protected PSource(String pSrcName)
|
||||
{
|
||||
if (pSrcName == null)
|
||||
{
|
||||
throw new NullPointerException("pSrcName is null");
|
||||
}
|
||||
this.pSrcName = pSrcName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the PSource algorithm name.
|
||||
*
|
||||
* @return the PSource algorithm name.
|
||||
*/
|
||||
public String getAlgorithm()
|
||||
{
|
||||
return pSrcName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
/**
|
||||
* This class specifies the parameters used with the
|
||||
* <a href="http://www.rsa.com/rsalabs/newfaq/q75.html"><i>RC2</i></a>
|
||||
* algorithm.
|
||||
* <p>
|
||||
* The parameters consist of an effective key size and optionally
|
||||
* an 8-byte initialization vector (IV) (only in feedback mode).
|
||||
* <p>
|
||||
* This class can be used to initialize a <code>Cipher</code> object that
|
||||
* implements the <i>RC2</i> algorithm.
|
||||
*/
|
||||
public class RC2ParameterSpec
|
||||
implements AlgorithmParameterSpec
|
||||
{
|
||||
private int effectiveKeyBits;
|
||||
private byte[] iv = new byte[8];
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for RC2 from the given effective key size
|
||||
* (in bits).
|
||||
*
|
||||
* @param effectiveKeyBits the effective key size in bits.
|
||||
*/
|
||||
public RC2ParameterSpec(
|
||||
int effectiveKeyBits)
|
||||
{
|
||||
this.effectiveKeyBits = effectiveKeyBits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for RC2 from the given effective key size
|
||||
* (in bits) and an 8-byte IV.
|
||||
* <p>
|
||||
* The bytes that constitute the IV are those between
|
||||
* <code>iv[0]</code> and <code>iv[7]</code> inclusive.
|
||||
*
|
||||
* @param effectiveKeyBits the effective key size in bits.
|
||||
* @param iv the buffer with the 8-byte IV.
|
||||
*/
|
||||
public RC2ParameterSpec(
|
||||
int effectiveKeyBits,
|
||||
byte[] iv)
|
||||
{
|
||||
this(effectiveKeyBits, iv, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for RC2 from the given effective key size
|
||||
* (in bits) and IV.
|
||||
* <p>
|
||||
* The IV is taken from <code>iv</code>, starting at
|
||||
* <code>offset</code> inclusive.
|
||||
* The bytes that constitute the IV are those between
|
||||
* <code>iv[offset]</code> and <code>iv[offset+7]</code> inclusive.
|
||||
*
|
||||
* @param effectiveKeyBits the effective key size in bits.
|
||||
* @param iv the buffer with the IV.
|
||||
* @param offset the offset in <code>iv</code> where the 8-byte IV starts.
|
||||
*/
|
||||
public RC2ParameterSpec(
|
||||
int effectiveKeyBits,
|
||||
byte[] iv,
|
||||
int offset)
|
||||
{
|
||||
this.effectiveKeyBits = effectiveKeyBits;
|
||||
|
||||
this.iv = new byte[8];
|
||||
System.arraycopy(iv, offset, this.iv, 0, this.iv.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the effective key size in bits.
|
||||
*
|
||||
* @return the effective key size in bits.
|
||||
*/
|
||||
public int getEffectiveKeyBits()
|
||||
{
|
||||
return effectiveKeyBits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the IV or null if this parameter set does not contain an IV.
|
||||
*
|
||||
* @return the IV or null if this parameter set does not contain an IV.
|
||||
*/
|
||||
public byte[] getIV()
|
||||
{
|
||||
if (iv == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] tmp = new byte[iv.length];
|
||||
|
||||
System.arraycopy(iv, 0, tmp, 0, tmp.length);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for equality between the specified object and this
|
||||
* object. Two RC2ParameterSpec objects are considered equal if their
|
||||
* effective key sizes and IVs are equal.
|
||||
* (Two IV references are considered equal if both are <tt>null</tt>.)
|
||||
*
|
||||
* @param obj the object to test for equality with this object.
|
||||
* @return true if the objects are considered equal, false otherwise.
|
||||
* @override equals in class java.lang.Object
|
||||
*/
|
||||
public boolean equals(
|
||||
Object obj)
|
||||
{
|
||||
if ((obj == null) || !(obj instanceof RC2ParameterSpec))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
RC2ParameterSpec spec = (RC2ParameterSpec)obj;
|
||||
|
||||
if (this.effectiveKeyBits != spec.effectiveKeyBits)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (iv != null)
|
||||
{
|
||||
if (spec.iv == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i != iv.length; i++)
|
||||
{
|
||||
if (iv[i] != spec.iv[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (spec.iv != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a hash code value for the object.
|
||||
* Objects that are equal will also have the same hashcode.
|
||||
*
|
||||
* @override hashCode in class java.lang.Object
|
||||
*/
|
||||
public int hashCode()
|
||||
{
|
||||
throw new RuntimeException("Not yet implemented");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
/**
|
||||
* This class specifies the parameters used with the
|
||||
* <a href="http://www.rsa.com/rsalabs/newfaq/q76.html"><i>RC5</i></a>
|
||||
* algorithm.
|
||||
* <p>
|
||||
* The parameters consist of a version number, a rounds count, a word
|
||||
* size, and optionally an initialization vector (IV) (only in feedback mode).
|
||||
* <p>
|
||||
* This class can be used to initialize a <code>Cipher</code> object that
|
||||
* implements the <i>RC5</i> algorithm as supplied by
|
||||
* <a href="http://www.rsa.com">RSA Data Security, Inc.</a> (RSA DSI),
|
||||
* or any parties authorized by RSA DSI.
|
||||
*/
|
||||
public class RC5ParameterSpec
|
||||
implements AlgorithmParameterSpec
|
||||
{
|
||||
private int version;
|
||||
private int rounds;
|
||||
private int wordSize;
|
||||
|
||||
private byte[] iv;
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for RC5 from the given version, number of
|
||||
* rounds and word size (in bits).
|
||||
*
|
||||
* @param version the version.
|
||||
* @param rounds the number of rounds.
|
||||
* @param wordSize the word size in bits.
|
||||
*/
|
||||
public RC5ParameterSpec(
|
||||
int version,
|
||||
int rounds,
|
||||
int wordSize)
|
||||
{
|
||||
this.version = version;
|
||||
this.rounds = rounds;
|
||||
this.wordSize = wordSize;
|
||||
this.iv = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for RC5 from the given version, number of
|
||||
* rounds, word size (in bits), and IV.
|
||||
* <p>
|
||||
* Note that the size of the IV (block size) must be twice the word
|
||||
* size. The bytes that constitute the IV are those between
|
||||
* <code>iv[0]</code> and <code>iv[2*(wordSize/8)-1]</code> inclusive.
|
||||
*
|
||||
* @param version the version.
|
||||
* @param rounds the number of rounds.
|
||||
* @param wordSize the word size in bits.
|
||||
* @param iv the buffer with the IV.
|
||||
*/
|
||||
public RC5ParameterSpec(
|
||||
int version,
|
||||
int rounds,
|
||||
int wordSize,
|
||||
byte[] iv)
|
||||
{
|
||||
this(version, rounds, wordSize, iv, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for RC5 from the given version, number of
|
||||
* rounds, word size (in bits), and IV.
|
||||
* <p>
|
||||
* The IV is taken from <code>iv</code>, starting at <code>offset</code> inclusive.
|
||||
* Note that the size of the IV (block size), starting at
|
||||
* <code>offset</code> inclusive, must be twice the word size.
|
||||
* The bytes that constitute the IV are those between
|
||||
* <code>iv[offset]</code> and <code>iv[offset+2*(wordSize/8)-1]</code>
|
||||
* inclusive.
|
||||
*
|
||||
* @param version the version.
|
||||
* @param rounds the number of rounds.
|
||||
* @param wordSize the word size in bits.
|
||||
* @param iv the buffer with the IV.
|
||||
* @param offset the offset in <code>iv</code> where the IV starts.
|
||||
*/
|
||||
public RC5ParameterSpec(
|
||||
int version,
|
||||
int rounds,
|
||||
int wordSize,
|
||||
byte[] iv,
|
||||
int offset)
|
||||
{
|
||||
this.version = version;
|
||||
this.rounds = rounds;
|
||||
this.wordSize = wordSize;
|
||||
this.iv = new byte[2 * (wordSize / 8)];
|
||||
|
||||
System.arraycopy(iv, offset, this.iv, 0, this.iv.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the version.
|
||||
*
|
||||
* @return the version.
|
||||
*/
|
||||
public int getVersion()
|
||||
{
|
||||
return version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of rounds.
|
||||
*
|
||||
* @return the number of rounds.
|
||||
*/
|
||||
public int getRounds()
|
||||
{
|
||||
return rounds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the word size in bits
|
||||
*
|
||||
* @return the word size in bits.
|
||||
*/
|
||||
public int getWordSize()
|
||||
{
|
||||
return wordSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the IV or null if this parameter set does not contain an IV.
|
||||
*
|
||||
* @return the IV or null if this parameter set does not contain an IV.
|
||||
*/
|
||||
public byte[] getIV()
|
||||
{
|
||||
if (iv == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] tmp = new byte[iv.length];
|
||||
|
||||
System.arraycopy(iv, 0, tmp, 0, iv.length);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for equality between the specified object and this
|
||||
* object. Two RC5ParameterSpec objects are considered equal if their
|
||||
* version numbers, number of rounds, word sizes, and IVs are equal.
|
||||
* (Two IV references are considered equal if both are <tt>null</tt>.)
|
||||
*
|
||||
* @param obj the object to test for equality with this object.
|
||||
* @return true if the objects are considered equal, false otherwise.
|
||||
*/
|
||||
public boolean equals(
|
||||
Object obj)
|
||||
{
|
||||
if ((obj == null) || !(obj instanceof RC5ParameterSpec))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
RC5ParameterSpec spec = (RC5ParameterSpec)obj;
|
||||
|
||||
if (this.version != spec.version)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.rounds != spec.rounds)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.wordSize != spec.wordSize)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (iv != null)
|
||||
{
|
||||
if (spec.iv == null || spec.iv.length != iv.length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i != iv.length; i++)
|
||||
{
|
||||
if (iv[i] != spec.iv[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (spec.iv != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a hash code value for the object.
|
||||
* Objects that are equal will also have the same hashcode.
|
||||
*/
|
||||
public int hashCode()
|
||||
{
|
||||
int code = version ^ rounds ^ wordSize;
|
||||
|
||||
if (iv != null)
|
||||
{
|
||||
for (int i = 0; i != iv.length; i++)
|
||||
{
|
||||
code ^= iv[i] << (8 * (i % 4));
|
||||
}
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package javax.crypto.spec;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.security.spec.KeySpec;
|
||||
|
||||
/**
|
||||
* This class specifies a secret key in a provider-independent fashion.
|
||||
* <p>
|
||||
* It can be used to construct a <code>SecretKey</code> from a byte array,
|
||||
* without having to go through a (provider-based)
|
||||
* <code>SecretKeyFactory</code>.
|
||||
* <p>
|
||||
* This class is only useful for raw secret keys that can be represented as
|
||||
* a byte array and have no key parameters associated with them, e.g., DES or
|
||||
* Triple DES keys.
|
||||
*
|
||||
* @see SecretKey
|
||||
* @see javax.crypto.SecretKeyFactory
|
||||
*/
|
||||
public class SecretKeySpec
|
||||
implements KeySpec, SecretKey
|
||||
{
|
||||
private static final long serialVersionUID = 6577238317307289933L;
|
||||
|
||||
private String algorithm;
|
||||
private byte[] key;
|
||||
|
||||
/**
|
||||
* Constructs a secret key from the given byte array.
|
||||
* <p>
|
||||
* This constructor does not check if the given bytes indeed specify a
|
||||
* secret key of the specified algorithm. For example, if the algorithm is
|
||||
* DES, this constructor does not check if <code>key</code> is 8 bytes
|
||||
* long, and also does not check for weak or semi-weak keys.
|
||||
* In order for those checks to be performed, an algorithm-specific
|
||||
* <i>key specification</i> class (in this case:
|
||||
* <a href = "DESKeySpec.html"><code>DESKeySpec</code></a>)
|
||||
* should be used.
|
||||
*
|
||||
* @param key the key material of the secret key.
|
||||
* @param algorithm the name of the secret-key algorithm to be associated
|
||||
* See Appendix A in the Java Cryptography Extension API Specification & Reference
|
||||
* for information about standard algorithm names.
|
||||
*/
|
||||
public SecretKeySpec(
|
||||
byte[] key,
|
||||
String algorithm)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw new IllegalArgumentException("null key passed");
|
||||
}
|
||||
|
||||
if (algorithm == null)
|
||||
{
|
||||
throw new IllegalArgumentException("null algorithm passed");
|
||||
}
|
||||
|
||||
this.key = new byte[key.length];
|
||||
System.arraycopy(key, 0, this.key, 0, key.length);
|
||||
this.algorithm = algorithm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a secret key from the given byte array, using the first
|
||||
* <code>len</code> bytes of <code>key</code>, starting at
|
||||
* <code>offset</code> inclusive.
|
||||
* <p>
|
||||
* The bytes that constitute the secret key are those between <code>key[offset]</code> and
|
||||
* <code>key[offset+len-1]</code> inclusive.
|
||||
* <p>
|
||||
* This constructor does not check if the given bytes indeed specify a
|
||||
* secret key of the specified algorithm. For example, if the algorithm is
|
||||
* DES, this constructor does not check if <code>key</code> is 8 bytes
|
||||
* long, and also does not check for weak or semi-weak keys.
|
||||
* In order for those checks to be performed, an algorithm-specific key
|
||||
* specification class (in this case: <a href = "DESKeySpec.html"><code>DESKeySpec</code></a>)
|
||||
* must be used.
|
||||
*
|
||||
* @param key the key material of the secret key.
|
||||
* @param offset the offset in <code>key</code> where the key material starts.
|
||||
* @param len the length of the key material.
|
||||
* @param algorithm the name of the secret-key algorithm to be associated
|
||||
* with the given key material. See Appendix A in the Java Cryptography Extension API
|
||||
* Specification & Reference for information about standard algorithm names.
|
||||
*/
|
||||
public SecretKeySpec(
|
||||
byte[] key,
|
||||
int offset,
|
||||
int len,
|
||||
String algorithm)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Null key passed");
|
||||
}
|
||||
|
||||
if ((key.length - offset) < len)
|
||||
{
|
||||
throw new IllegalArgumentException("Bad offset/len");
|
||||
}
|
||||
|
||||
if (algorithm == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Null algorithm string passed");
|
||||
}
|
||||
|
||||
this.key = new byte[len];
|
||||
System.arraycopy(key, offset, this.key, 0, len);
|
||||
this.algorithm = algorithm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the algorithm associated with this secret key.
|
||||
*
|
||||
* @return the secret key algorithm.
|
||||
*/
|
||||
public String getAlgorithm()
|
||||
{
|
||||
return algorithm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the encoding format for this secret key.
|
||||
*
|
||||
* @return the string "RAW".
|
||||
*/
|
||||
public java.lang.String getFormat()
|
||||
{
|
||||
return "RAW";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the key material of this secret key.
|
||||
*
|
||||
* @return the key material
|
||||
*/
|
||||
public byte[] getEncoded()
|
||||
{
|
||||
byte[] tmp = new byte[key.length];
|
||||
|
||||
System.arraycopy(key, 0, tmp, 0, tmp.length);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a hash code value for the object.
|
||||
* Objects that are equal will also have the same hashcode.
|
||||
*/
|
||||
public int hashCode()
|
||||
{
|
||||
int code = algorithm.toUpperCase().hashCode();
|
||||
|
||||
for (int i = 0; i != this.key.length; i++)
|
||||
{
|
||||
code ^= this.key[i] << (8 * (i % 4));
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
public boolean equals(
|
||||
Object obj)
|
||||
{
|
||||
if ((obj == null) || !(obj instanceof SecretKeySpec))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
SecretKeySpec spec = (SecretKeySpec)obj;
|
||||
|
||||
if (!this.algorithm.equalsIgnoreCase(spec.algorithm))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.key.length != spec.key.length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i != this.key.length; i++)
|
||||
{
|
||||
if (this.key[i] != spec.key[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user