aboutsummaryrefslogtreecommitdiff
path: root/libjava/classpath/javax/crypto
diff options
context:
space:
mode:
authorTom Tromey <tromey@gcc.gnu.org>2007-01-09 19:58:05 +0000
committerTom Tromey <tromey@gcc.gnu.org>2007-01-09 19:58:05 +0000
commit97b8365cafc3a344a22d3980b8ed885f5c6d8357 (patch)
tree996a5f57d4a68c53473382e45cb22f574cb3e4db /libjava/classpath/javax/crypto
parentc648dedbde727ca3f883bb5fd773aa4af70d3369 (diff)
downloadgcc-97b8365cafc3a344a22d3980b8ed885f5c6d8357.zip
gcc-97b8365cafc3a344a22d3980b8ed885f5c6d8357.tar.gz
gcc-97b8365cafc3a344a22d3980b8ed885f5c6d8357.tar.bz2
Merged gcj-eclipse branch to trunk.
From-SVN: r120621
Diffstat (limited to 'libjava/classpath/javax/crypto')
-rw-r--r--libjava/classpath/javax/crypto/Cipher.java199
-rw-r--r--libjava/classpath/javax/crypto/CipherOutputStream.java180
-rw-r--r--libjava/classpath/javax/crypto/ExemptionMechanism.java117
-rw-r--r--libjava/classpath/javax/crypto/KeyAgreement.java116
-rw-r--r--libjava/classpath/javax/crypto/KeyGenerator.java114
-rw-r--r--libjava/classpath/javax/crypto/Mac.java131
-rw-r--r--libjava/classpath/javax/crypto/MacSpi.java18
-rw-r--r--libjava/classpath/javax/crypto/SecretKeyFactory.java108
8 files changed, 483 insertions, 500 deletions
diff --git a/libjava/classpath/javax/crypto/Cipher.java b/libjava/classpath/javax/crypto/Cipher.java
index 1b56a07..7c18e6f 100644
--- a/libjava/classpath/javax/crypto/Cipher.java
+++ b/libjava/classpath/javax/crypto/Cipher.java
@@ -157,161 +157,159 @@ public class Cipher
/** Our current state (encrypting, wrapping, etc.) */
private int state;
-
- // Class methods.
- // ------------------------------------------------------------------------
-
/**
- * <p>Creates a new cipher instance for the given transformation.</p>
- *
- * <p>The installed providers are tried in order for an
- * implementation, and the first appropriate instance is returned. If
- * no installed provider can provide the implementation, an
- * appropriate exception is thrown.</p>
- *
+ * Creates a new cipher instance for the given transformation.
+ * <p>
+ * The installed providers are tried in order for an implementation, and the
+ * first appropriate instance is returned. If no installed provider can
+ * provide the implementation, an appropriate exception is thrown.
+ *
* @param transformation The transformation to create.
* @return An appropriate cipher for this transformation.
- * @throws java.security.NoSuchAlgorithmException If no installed
- * provider can supply the appropriate cipher or mode.
- * @throws javax.crypto.NoSuchPaddingException If no installed
- * provider can supply the appropriate padding.
+ * @throws NoSuchAlgorithmException If no installed provider can supply the
+ * appropriate cipher or mode.
+ * @throws NoSuchPaddingException If no installed provider can supply the
+ * appropriate padding.
*/
public static final Cipher getInstance(String transformation)
- throws NoSuchAlgorithmException, NoSuchPaddingException
+ throws NoSuchAlgorithmException, NoSuchPaddingException
{
- Provider[] providers = Security.getProviders();
- NoSuchPaddingException ex = null;
- String msg = "";
- for (int i = 0; i < providers.length; i++)
- {
- try
- {
- return getInstance(transformation, providers[i]);
- }
- catch (NoSuchAlgorithmException nsae)
- {
- msg = nsae.getMessage();
- ex = null;
- }
- catch (NoSuchPaddingException nspe)
- {
- ex = nspe;
- }
- }
- if (ex != null)
- {
- throw ex;
- }
- throw new NoSuchAlgorithmException(msg);
+ Provider[] p = Security.getProviders();
+ NoSuchAlgorithmException lastException = null;
+ NoSuchPaddingException lastPaddingException = null;
+ for (int i = 0; i < p.length; i++)
+ try
+ {
+ return getInstance(transformation, p[i]);
+ }
+ catch (NoSuchAlgorithmException x)
+ {
+ lastException = x;
+ lastPaddingException = null;
+ }
+ catch (NoSuchPaddingException x)
+ {
+ lastPaddingException = x;
+ }
+ if (lastPaddingException != null)
+ throw lastPaddingException;
+ if (lastException != null)
+ throw lastException;
+ throw new NoSuchAlgorithmException(transformation);
}
/**
- * <p>Creates a new cipher instance for the given transformation and
- * the named provider.</p>
- *
+ * Creates a new cipher instance for the given transformation and the named
+ * provider.
+ *
* @param transformation The transformation to create.
- * @param provider The name of the provider to use.
+ * @param provider The name of the provider to use.
* @return An appropriate cipher for this transformation.
- * @throws java.security.NoSuchAlgorithmException If the provider cannot
- * supply the appropriate cipher or mode.
- * @throws java.security.NoSuchProviderException If the named provider
- * is not installed.
- * @throws javax.crypto.NoSuchPaddingException If the provider cannot
- * supply the appropriate padding.
+ * @throws NoSuchAlgorithmException If the provider cannot supply the
+ * appropriate cipher or mode.
+ * @throws NoSuchProviderException If the named provider is not installed.
+ * @throws NoSuchPaddingException If the provider cannot supply the
+ * appropriate padding.
+ * @throws IllegalArgumentException if either <code>transformation</code> or
+ * <code>provider</code> is <code>null</code>.
*/
public static final Cipher getInstance(String transformation, String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException,
- NoSuchPaddingException
+ throws NoSuchAlgorithmException, NoSuchProviderException,
+ NoSuchPaddingException
{
+ if (provider == null)
+ throw new IllegalArgumentException("provider MUST NOT be null");
Provider p = Security.getProvider(provider);
if (p == null)
- {
- throw new NoSuchProviderException(provider);
- }
+ throw new NoSuchProviderException(provider);
return getInstance(transformation, p);
}
/**
- * Creates a new cipher instance for the given transform and the given
+ * Creates a new cipher instance for a given transformation from a given
* provider.
- *
+ *
* @param transformation The transformation to create.
- * @param provider The provider to use.
+ * @param provider The provider to use.
* @return An appropriate cipher for this transformation.
- * @throws java.security.NoSuchAlgorithmException If the given
- * provider cannot supply the appropriate cipher or mode.
- * @throws javax.crypto.NoSuchPaddingException If the given
- * provider cannot supply the appropriate padding scheme.
+ * @throws NoSuchAlgorithmException If the given provider cannot supply the
+ * appropriate cipher or mode.
+ * @throws NoSuchPaddingException If the given provider cannot supply the
+ * appropriate padding scheme.
*/
- public static final Cipher getInstance(String transformation, Provider provider)
- throws NoSuchAlgorithmException, NoSuchPaddingException
+ public static final Cipher getInstance(String transformation,
+ Provider provider)
+ throws NoSuchAlgorithmException, NoSuchPaddingException
{
- CipherSpi result = null;
- String key = null;
- String alg = null, mode = null, pad = null;
- String msg = "";
+ StringBuilder sb = new StringBuilder().append("Cipher transformation [")
+ .append(transformation).append("] from provider [")
+ .append(provider).append("] ");
+ Throwable cause;
+ Object spi;
+ CipherSpi result;
if (transformation.indexOf('/') < 0)
{
try
{
- result = (CipherSpi) Engine.getInstance(SERVICE, transformation,
- provider);
- return new Cipher(result, provider, transformation);
+ spi = Engine.getInstance(SERVICE, transformation, provider);
+ return new Cipher((CipherSpi) spi, provider, transformation);
}
catch (Exception e)
{
- msg = e.getMessage();
+ if (e instanceof NoSuchAlgorithmException)
+ throw (NoSuchAlgorithmException) e;
+ cause = e;
}
}
else
{
StringTokenizer tok = new StringTokenizer(transformation, "/");
if (tok.countTokens() != 3)
- {
- throw new NoSuchAlgorithmException("badly formed transformation");
- }
- alg = tok.nextToken();
- mode = tok.nextToken();
- pad = tok.nextToken();
+ throw new NoSuchAlgorithmException(sb.append("is malformed").toString());
+
+ String alg = tok.nextToken();
+ String mode = tok.nextToken();
+ String pad = tok.nextToken();
try
{
- result = (CipherSpi) Engine.getInstance(SERVICE, transformation,
- provider);
- return new Cipher(result, provider, transformation);
+ spi = Engine.getInstance(SERVICE, transformation, provider);
+ return new Cipher((CipherSpi) spi, provider, transformation);
}
catch (Exception e)
{
- msg = e.getMessage();
+ cause = e;
}
+
try
{
- result = (CipherSpi) Engine.getInstance(SERVICE, alg + '/' + mode,
- provider);
+ spi = Engine.getInstance(SERVICE, alg + '/' + mode, provider);
+ result = (CipherSpi) spi;
result.engineSetPadding(pad);
return new Cipher(result, provider, transformation);
}
catch (Exception e)
{
if (e instanceof NoSuchPaddingException)
- {
- throw (NoSuchPaddingException) e;
- }
- msg = e.getMessage();
+ throw (NoSuchPaddingException) e;
+ cause = e;
}
+
try
{
- result = (CipherSpi) Engine.getInstance(SERVICE, alg + "//" + pad,
- provider);
+ spi = Engine.getInstance(SERVICE, alg + "//" + pad, provider);
+ result = (CipherSpi) spi;
result.engineSetMode(mode);
return new Cipher(result, provider, transformation);
}
catch (Exception e)
{
- msg = e.getMessage();
+ cause = e;
}
+
try
{
- result = (CipherSpi) Engine.getInstance(SERVICE, alg, provider);
+ spi = Engine.getInstance(SERVICE, alg, provider);
+ result = (CipherSpi) spi;
result.engineSetMode(mode);
result.engineSetPadding(pad);
return new Cipher(result, provider, transformation);
@@ -319,18 +317,16 @@ public class Cipher
catch (Exception e)
{
if (e instanceof NoSuchPaddingException)
- {
- throw (NoSuchPaddingException) e;
- }
- msg = e.getMessage();
+ throw (NoSuchPaddingException) e;
+ cause = e;
}
}
- throw new NoSuchAlgorithmException(transformation + ": " + msg);
+ sb.append("could not be created");
+ NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
+ x.initCause(cause);
+ throw x;
}
- // Constructor.
- // ------------------------------------------------------------------------
-
/**
* Create a cipher.
*
@@ -347,9 +343,6 @@ public class Cipher
state = INITIAL_STATE;
}
- // Public instance methods.
- // ------------------------------------------------------------------------
-
/**
* Get the name that this cipher instance was created with; this is
* equivalent to the "transformation" argument given to any of the
diff --git a/libjava/classpath/javax/crypto/CipherOutputStream.java b/libjava/classpath/javax/crypto/CipherOutputStream.java
index adeb6e5..5d1e57a 100644
--- a/libjava/classpath/javax/crypto/CipherOutputStream.java
+++ b/libjava/classpath/javax/crypto/CipherOutputStream.java
@@ -45,59 +45,25 @@ import java.io.OutputStream;
/**
* A filtered output stream that transforms data written to it with a
* {@link Cipher} before sending it to the underlying output stream.
- *
+ *
* @author Casey Marshall (csm@gnu.org)
*/
public class CipherOutputStream extends FilterOutputStream
{
-
- // Fields.
- // ------------------------------------------------------------------------
-
/** The underlying cipher. */
private Cipher cipher;
- private byte[][] inBuffer;
-
- private int inLength;
-
- private byte[] outBuffer;
-
- private static final int FIRST_TIME = 0;
- private static final int SECOND_TIME = 1;
- private static final int SEASONED = 2;
- private int state;
-
- /** True if the cipher is a stream cipher (blockSize == 1) */
- private boolean isStream;
-
- // Constructors.
- // ------------------------------------------------------------------------
-
/**
- * Create a new cipher output stream. The cipher argument must have
- * already been initialized.
- *
- * @param out The sink for transformed data.
+ * Create a new cipher output stream. The cipher argument must have already
+ * been initialized.
+ *
+ * @param out The sink for transformed data.
* @param cipher The cipher to transform data with.
*/
public CipherOutputStream(OutputStream out, Cipher cipher)
{
super(out);
- if (cipher != null)
- {
- this.cipher = cipher;
- if (!(isStream = cipher.getBlockSize() == 1))
- {
- inBuffer = new byte[2][];
- inBuffer[0] = new byte[cipher.getBlockSize()];
- inBuffer[1] = new byte[cipher.getBlockSize()];
- inLength = 0;
- state = FIRST_TIME;
- }
- }
- else
- this.cipher = new NullCipher();
+ this.cipher = (cipher != null) ? cipher : new NullCipher();
}
/**
@@ -110,52 +76,36 @@ public class CipherOutputStream extends FilterOutputStream
super(out);
}
- // Instance methods.
- // ------------------------------------------------------------------------
-
/**
* Close this output stream, and the sink output stream.
- *
- * <p>This method will first invoke the {@link Cipher#doFinal()}
- * method of the underlying {@link Cipher}, and writes the output of
- * that method to the sink output stream.
- *
- * @throws java.io.IOException If an I/O error occurs, or if an error
- * is caused by finalizing the transformation.
+ * <p>
+ * This method will first invoke the {@link Cipher#doFinal()} method of the
+ * underlying {@link Cipher}, and writes the output of that method to the
+ * sink output stream.
+ *
+ * @throws IOException If an I/O error occurs, or if an error is caused by
+ * finalizing the transformation.
*/
public void close() throws IOException
{
try
{
- int len;
- if (state != FIRST_TIME)
- {
- len = cipher.update(inBuffer[0], 0, inBuffer[0].length, outBuffer);
- out.write(outBuffer, 0, len);
- }
- len = cipher.doFinal(inBuffer[0], 0, inLength, outBuffer);
- out.write(outBuffer, 0, len);
- }
- catch (javax.crypto.IllegalBlockSizeException ibse)
- {
- throw new IOException(ibse.toString());
+ out.write(cipher.doFinal());
+ out.flush();
+ out.close();
}
- catch (javax.crypto.BadPaddingException bpe)
+ catch (Exception cause)
{
- throw new IOException(bpe.toString());
+ IOException ioex = new IOException(String.valueOf(cause));
+ ioex.initCause(cause);
+ throw ioex;
}
- catch (ShortBufferException sbe)
- {
- throw new IOException(sbe.toString());
- }
- out.flush();
- out.close();
}
/**
* Flush any pending output.
*
- * @throws java.io.IOException If an I/O error occurs.
+ * @throws IOException If an I/O error occurs.
*/
public void flush() throws IOException
{
@@ -164,40 +114,22 @@ public class CipherOutputStream extends FilterOutputStream
/**
* Write a single byte to the output stream.
- *
+ *
* @param b The next byte.
- * @throws java.io.IOException If an I/O error occurs, or if the
- * underlying cipher is not in the correct state to transform
- * data.
+ * @throws IOException If an I/O error occurs, or if the underlying cipher is
+ * not in the correct state to transform data.
*/
public void write(int b) throws IOException
{
- if (isStream)
- {
- byte[] buf = new byte[] { (byte) b };
- try
- {
- cipher.update(buf, 0, 1, buf, 0);
- }
- catch (ShortBufferException sbe)
- {
- throw new IOException(sbe.toString());
- }
- out.write(buf);
- return;
- }
- inBuffer[1][inLength++] = (byte) b;
- if (inLength == inBuffer[1].length)
- process();
+ write(new byte[] { (byte) b }, 0, 1);
}
/**
* Write a byte array to the output stream.
- *
+ *
* @param buf The next bytes.
- * @throws java.io.IOException If an I/O error occurs, or if the
- * underlying cipher is not in the correct state to transform
- * data.
+ * @throws IOException If an I/O error occurs, or if the underlying cipher is
+ * not in the correct state to transform data.
*/
public void write(byte[] buf) throws IOException
{
@@ -206,63 +138,15 @@ public class CipherOutputStream extends FilterOutputStream
/**
* Write a portion of a byte array to the output stream.
- *
+ *
* @param buf The next bytes.
* @param off The offset in the byte array to start.
* @param len The number of bytes to write.
- * @throws java.io.IOException If an I/O error occurs, or if the
- * underlying cipher is not in the correct state to transform
- * data.
+ * @throws IOException If an I/O error occurs, or if the underlying cipher is
+ * not in the correct state to transform data.
*/
public void write(byte[] buf, int off, int len) throws IOException
{
- if (isStream)
- {
- out.write(cipher.update(buf, off, len));
- return;
- }
- int count = 0;
- while (count < len)
- {
- int l = Math.min(inBuffer[1].length - inLength, len - count);
- System.arraycopy(buf, off+count, inBuffer[1], inLength, l);
- count += l;
- inLength += l;
- if (inLength == inBuffer[1].length)
- process();
- }
- }
-
- // Own method.
- // -------------------------------------------------------------------------
-
- private void process() throws IOException
- {
- if (state == SECOND_TIME)
- {
- state = SEASONED;
- }
- else
- {
- byte[] temp = inBuffer[0];
- inBuffer[0] = inBuffer[1];
- inBuffer[1] = temp;
- }
- if (state == FIRST_TIME)
- {
- inLength = 0;
- state = SECOND_TIME;
- return;
- }
- try
- {
- cipher.update(inBuffer[0], 0, inBuffer[0].length, outBuffer);
- }
- catch (ShortBufferException sbe)
- {
- throw new IOException(sbe.toString());
- }
- out.write(outBuffer);
- inLength = 0;
+ out.write(cipher.update(buf, off, len));
}
}
diff --git a/libjava/classpath/javax/crypto/ExemptionMechanism.java b/libjava/classpath/javax/crypto/ExemptionMechanism.java
index b6cb02c..baf6bad 100644
--- a/libjava/classpath/javax/crypto/ExemptionMechanism.java
+++ b/libjava/classpath/javax/crypto/ExemptionMechanism.java
@@ -87,66 +87,111 @@ public class ExemptionMechanism
virgin = true;
}
- // Class methods.
- // ------------------------------------------------------------------------
-
+ /**
+ * Create an instance of <code>ExemptionMechanism</code> for a designated
+ * <code>mechanism</code> from the first Security Provider offering it.
+ *
+ * @param mechanism the name of the exemption mechanism to create.
+ * @return a newly created instance of <code>ExemptionMechanism</code>.
+ * @throws IllegalArgumentException if the provider is null.
+ * @throws NoSuchAlgorithmException if no such exemption mechanism is
+ * available from any known Security Provider.
+ * @throws IllegalArgumentException if <code>mechanism</code> is
+ * <code>null</code> or is an empty string.
+ */
public static final ExemptionMechanism getInstance(String mechanism)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
- Provider[] provs = Security.getProviders();
- String msg = "";
- for (int i = 0; i < provs.length; i++)
- {
- try
- {
- return getInstance(mechanism, provs[i]);
- }
- catch (NoSuchAlgorithmException nsae)
- {
- msg = nsae.getMessage();
- }
- }
- throw new NoSuchAlgorithmException(msg);
+ Provider[] p = Security.getProviders();
+ NoSuchAlgorithmException lastException = null;
+ for (int i = 0; i < p.length; i++)
+ try
+ {
+ return getInstance(mechanism, p[i]);
+ }
+ catch (NoSuchAlgorithmException x)
+ {
+ lastException = x;
+ }
+ if (lastException != null)
+ throw lastException;
+ throw new NoSuchAlgorithmException(mechanism);
}
+ /**
+ * Create an instance of <code>ExemptionMechanism</code> for a designated
+ * <code>mechanism</code> from a named <code>provider</code>.
+ *
+ * @param mechanism the name of the exemption mechanism to create.
+ * @param provider the security provider to provide the exemption
+ * <code>mechanism</code>.
+ * @return a newly created instance of <code>ExemptionMechanism</code>.
+ * @throws NoSuchAlgorithmException if no such exemption mechanism is
+ * available from the named <code>provider</code>.
+ * @throws NoSuchProviderException if no Security Provider with the designated
+ * name is known to the underlying JVM.
+ * @throws IllegalArgumentException if either <code>mechanism</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>mechanism</code> is an empty string.
+ */
public static final ExemptionMechanism getInstance(String mechanism,
String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null)
+ throw new IllegalArgumentException("provider MUST NOT be null");
Provider p = Security.getProvider(provider);
if (p == null)
- {
- throw new NoSuchProviderException(provider);
- }
+ throw new NoSuchProviderException(provider);
return getInstance(mechanism, p);
}
+ /**
+ * Create an instance of <code>ExemptionMechanism</code> for a designated
+ * <code>mechanism</code> from a designated <code>provider</code>.
+ *
+ * @param mechanism the name of the exemption mechanism to create.
+ * @param provider the security provider to provide the exemption
+ * <code>mechanism</code>.
+ * @return a newly created instance of <code>ExemptionMechanism</code>.
+ * @throws NoSuchAlgorithmException if an exemption mechanism could not be
+ * created.
+ * @throws IllegalArgumentException if either <code>mechanism</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>mechanism</code> is an empty string.
+ */
public static final ExemptionMechanism getInstance(String mechanism,
Provider provider)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
+ StringBuilder sb = new StringBuilder("ExemptionMechanism [")
+ .append(mechanism).append("] from provider[")
+ .append(provider).append("] could not be created");
+ Throwable cause;
try
{
- return new ExemptionMechanism((ExemptionMechanismSpi)
- Engine.getInstance(SERVICE, mechanism, provider),
- provider, mechanism);
+ Object spi = Engine.getInstance(SERVICE, mechanism, provider);
+ return new ExemptionMechanism((ExemptionMechanismSpi) spi,
+ provider,
+ mechanism);
}
- catch (InvocationTargetException ite)
+ catch (InvocationTargetException x)
{
- if (ite.getCause() instanceof NoSuchAlgorithmException)
- throw (NoSuchAlgorithmException) ite.getCause();
- else
- throw new NoSuchAlgorithmException(mechanism);
+ cause = x.getCause();
+ if (cause instanceof NoSuchAlgorithmException)
+ throw (NoSuchAlgorithmException) cause;
+ if (cause == null)
+ cause = x;
}
- catch (ClassCastException cce)
+ catch (ClassCastException x)
{
- throw new NoSuchAlgorithmException(mechanism);
+ cause = x;
}
+ NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
+ x.initCause(cause);
+ throw x;
}
- // Instance methods.
- // ------------------------------------------------------------------------
-
public final byte[] genExemptionBlob()
throws IllegalStateException, ExemptionMechanismException
{
diff --git a/libjava/classpath/javax/crypto/KeyAgreement.java b/libjava/classpath/javax/crypto/KeyAgreement.java
index d71743e..4900310 100644
--- a/libjava/classpath/javax/crypto/KeyAgreement.java
+++ b/libjava/classpath/javax/crypto/KeyAgreement.java
@@ -101,97 +101,103 @@ public class KeyAgreement
virgin = true;
}
- // Class methods.
- // ------------------------------------------------------------------------
-
/**
* Get an implementation of an algorithm from the first provider that
* implements it.
- *
+ *
* @param algorithm The name of the algorithm to get.
* @return The proper KeyAgreement instacne, if found.
- * @throws java.security.NoSuchAlgorithmException If the specified
- * algorithm is not implemented by any installed provider.
+ * @throws NoSuchAlgorithmException If the specified algorithm is not
+ * implemented by any installed provider.
+ * @throws IllegalArgumentException if <code>algorithm</code> is
+ * <code>null</code> or is an empty string.
*/
public static final KeyAgreement getInstance(String algorithm)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
- Provider[] provs = Security.getProviders();
- String msg = algorithm;
- for (int i = 0; i < provs.length; i++)
- {
- try
- {
- return getInstance(algorithm, provs[i]);
- }
- catch (NoSuchAlgorithmException nsae)
- {
- msg = nsae.getMessage();
- }
- }
- throw new NoSuchAlgorithmException(msg);
+ Provider[] p = Security.getProviders();
+ NoSuchAlgorithmException lastException = null;
+ for (int i = 0; i < p.length; i++)
+ try
+ {
+ return getInstance(algorithm, p[i]);
+ }
+ catch (NoSuchAlgorithmException x)
+ {
+ lastException = x;
+ }
+ if (lastException != null)
+ throw lastException;
+ throw new NoSuchAlgorithmException(algorithm);
}
/**
- * Get an implementation of an algorithm from a named provider.
- *
- * @param algorithm The name of the algorithm to get.
- * @param provider The name of the provider from which to get the
- * implementation.
+ * Return an implementation of an algorithm from a named provider.
+ *
+ * @param algorithm The name of the algorithm to create.
+ * @param provider The name of the provider from which to get the
+ * implementation.
* @return The proper KeyAgreement instance, if found.
- * @throws java.security.NoSuchAlgorithmException If the named provider
- * does not implement the algorithm.
- * @throws java.security.NoSuchProviderException If the named provider
- * does not exist.
+ * @throws NoSuchAlgorithmException If the named provider does not implement
+ * the algorithm.
+ * @throws NoSuchProviderException If the named provider does not exist.
+ * @throws IllegalArgumentException if either <code>algorithm</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>algorithm</code> is an empty string.
*/
- public static final KeyAgreement getInstance(String algorithm,
- String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException
+ public static final KeyAgreement getInstance(String algorithm, String provider)
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null)
+ throw new IllegalArgumentException("provider MUST NOT be null");
Provider p = Security.getProvider(provider);
if (p == null)
- {
- throw new NoSuchProviderException(provider);
- }
+ throw new NoSuchProviderException(provider);
return getInstance(algorithm, p);
}
/**
- * Get an implementation of an algorithm from a specific provider.
- *
+ * Return an implementation of an algorithm from a specific provider.
+ *
* @param algorithm The name of the algorithm to get.
- * @param provider The provider from which to get the implementation.
+ * @param provider The provider from which to get the implementation.
* @return The proper KeyAgreement instance, if found.
- * @throws java.security.NoSuchAlgorithmException If this provider
- * does not implement the algorithm.
+ * @throws NoSuchAlgorithmException If this provider does not implement the
+ * algorithm.
+ * @throws IllegalArgumentException if either <code>algorithm</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>algorithm</code> is an empty string.
*/
public static final KeyAgreement getInstance(String algorithm,
Provider provider)
throws NoSuchAlgorithmException
{
+ StringBuilder sb = new StringBuilder("KeyAgreement algorithm [")
+ .append(algorithm).append("] from provider[")
+ .append(provider).append("] could not be created");
+ Throwable cause;
try
{
- return new KeyAgreement((KeyAgreementSpi)
- Engine.getInstance(SERVICE, algorithm, provider),
- provider, algorithm);
+ Object spi = Engine.getInstance(SERVICE, algorithm, provider);
+ return new KeyAgreement((KeyAgreementSpi) spi, provider, algorithm);
}
- catch (InvocationTargetException ite)
+ catch (InvocationTargetException x)
{
- if (ite.getCause() == null)
- throw new NoSuchAlgorithmException(algorithm);
- if (ite.getCause() instanceof NoSuchAlgorithmException)
- throw (NoSuchAlgorithmException) ite.getCause();
- throw new NoSuchAlgorithmException(algorithm);
+ cause = x.getCause();
+ if (cause instanceof NoSuchAlgorithmException)
+ throw (NoSuchAlgorithmException) cause;
+ if (cause == null)
+ cause = x;
}
- catch (ClassCastException cce)
+ catch (ClassCastException x)
{
- throw new NoSuchAlgorithmException(algorithm);
+ cause = x;
}
+ NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
+ x.initCause(cause);
+ throw x;
}
- // Instance methods.
- // ------------------------------------------------------------------------
-
/**
* Do a phase in the key agreement. The number of times this method is
* called depends upon the algorithm and the number of parties
diff --git a/libjava/classpath/javax/crypto/KeyGenerator.java b/libjava/classpath/javax/crypto/KeyGenerator.java
index e824c64..79334e9 100644
--- a/libjava/classpath/javax/crypto/KeyGenerator.java
+++ b/libjava/classpath/javax/crypto/KeyGenerator.java
@@ -94,95 +94,103 @@ public class KeyGenerator
this.algorithm = algorithm;
}
- // Class methods.
- // ------------------------------------------------------------------------
-
/**
- * Create a new key generator, returning the first available
- * implementation.
- *
+ * Create a new key generator, returning the first available implementation.
+ *
* @param algorithm The generator algorithm name.
- * @throws java.security.NoSuchAlgorithmException If the specified
- * algorithm does not exist.
+ * @throws NoSuchAlgorithmException If the specified algorithm does not exist.
+ * @throws IllegalArgumentException if <code>algorithm</code> is
+ * <code>null</code> or is an empty string.
*/
public static final KeyGenerator getInstance(String algorithm)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
- Provider[] provs = Security.getProviders();
- String msg = algorithm;
- for (int i = 0; i < provs.length; i++)
- {
- try
- {
- return getInstance(algorithm, provs[i]);
- }
- catch (NoSuchAlgorithmException nsae)
- {
- msg = nsae.getMessage();
- }
- }
- throw new NoSuchAlgorithmException(msg);
+ Provider[] p = Security.getProviders();
+ NoSuchAlgorithmException lastException = null;
+ for (int i = 0; i < p.length; i++)
+ try
+ {
+ return getInstance(algorithm, p[i]);
+ }
+ catch (NoSuchAlgorithmException x)
+ {
+ lastException = x;
+ }
+ if (lastException != null)
+ throw lastException;
+ throw new NoSuchAlgorithmException(algorithm);
}
/**
* Create a new key generator from the named provider.
- *
+ *
* @param algorithm The generator algorithm name.
- * @param provider The name of the provider to use.
+ * @param provider The name of the provider to use.
* @return An appropriate key generator, if found.
- * @throws java.security.NoSuchAlgorithmException If the specified
- * algorithm is not implemented by the named provider.
- * @throws java.security.NoSuchProviderException If the named provider
- * does not exist.
+ * @throws NoSuchAlgorithmException If the specified algorithm is not
+ * implemented by the named provider.
+ * @throws NoSuchProviderException If the named provider does not exist.
+ * @throws IllegalArgumentException if either <code>algorithm</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>algorithm</code> is an empty string.
*/
public static final KeyGenerator getInstance(String algorithm, String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null)
+ throw new IllegalArgumentException("provider MUST NOT be null");
Provider p = Security.getProvider(provider);
if (p == null)
- {
- throw new NoSuchProviderException(provider);
- }
+ throw new NoSuchProviderException(provider);
return getInstance(algorithm, p);
}
/**
* Create a new key generator from the supplied provider.
- *
+ *
* @param algorithm The generator algorithm name.
- * @param provider The provider to use.
+ * @param provider The provider to use.
* @return An appropriate key generator, if found.
- * @throws java.security.NoSuchAlgorithmException If the specified
- * algorithm is not implemented by the provider.
+ * @throws NoSuchAlgorithmException If the specified algorithm is not
+ * implemented by the provider.
+ * @throws IllegalArgumentException if either <code>algorithm</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>algorithm</code> is an empty string.
*/
- public static final KeyGenerator getInstance(String algorithm, Provider provider)
- throws NoSuchAlgorithmException
+ public static final KeyGenerator getInstance(String algorithm,
+ Provider provider)
+ throws NoSuchAlgorithmException
{
+ StringBuilder sb = new StringBuilder("KeyGenerator algorithm [")
+ .append(algorithm).append("] from provider[")
+ .append(provider).append("] could not be created");
+ Throwable cause;
try
{
- KeyGenerator instance = new KeyGenerator((KeyGeneratorSpi)
- Engine.getInstance(SERVICE, algorithm, provider),
- provider, algorithm);
+ Object spi = Engine.getInstance(SERVICE, algorithm, provider);
+ KeyGenerator instance = new KeyGenerator((KeyGeneratorSpi) spi,
+ provider,
+ algorithm);
instance.init(new SecureRandom());
return instance;
}
- catch (InvocationTargetException ite)
+ catch (InvocationTargetException x)
{
- if (ite.getCause() == null)
- throw new NoSuchAlgorithmException(algorithm);
- if (ite.getCause() instanceof NoSuchAlgorithmException)
- throw (NoSuchAlgorithmException) ite.getCause();
- throw new NoSuchAlgorithmException(algorithm);
+ cause = x.getCause();
+ if (cause instanceof NoSuchAlgorithmException)
+ throw (NoSuchAlgorithmException) cause;
+ if (cause == null)
+ cause = x;
}
- catch (ClassCastException cce)
+ catch (ClassCastException x)
{
- throw new NoSuchAlgorithmException(algorithm);
+ cause = x;
}
+ NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
+ x.initCause(cause);
+ throw x;
}
- // Instance methods.
- // ------------------------------------------------------------------------
-
/**
* Generate a key.
*
diff --git a/libjava/classpath/javax/crypto/Mac.java b/libjava/classpath/javax/crypto/Mac.java
index abbff8b..2a269ab 100644
--- a/libjava/classpath/javax/crypto/Mac.java
+++ b/libjava/classpath/javax/crypto/Mac.java
@@ -41,6 +41,7 @@ package javax.crypto;
import gnu.java.security.Engine;
import java.lang.reflect.InvocationTargetException;
+import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
@@ -107,96 +108,104 @@ public class Mac implements Cloneable
virgin = true;
}
- // Class methods.
- // ------------------------------------------------------------------------
-
/**
- * Get an instance of the named algorithm from the first provider with
- * an appropriate implementation.
- *
+ * Create an instance of the named algorithm from the first provider with an
+ * appropriate implementation.
+ *
* @param algorithm The name of the algorithm.
- * @return An appropriate Mac instance, if the specified algorithm
- * is implemented by a provider.
- * @throws java.security.NoSuchAlgorithmException If no implementation
- * of the named algorithm is installed.
+ * @return An appropriate Mac instance, if the specified algorithm is
+ * implemented by a provider.
+ * @throws NoSuchAlgorithmException If no implementation of the named
+ * algorithm is installed.
+ * @throws IllegalArgumentException if <code>algorithm</code> is
+ * <code>null</code> or is an empty string.
*/
public static final Mac getInstance(String algorithm)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
- Provider[] provs = Security.getProviders();
- String msg = "";
- for (int i = 0; i < provs.length; i++)
- {
- try
- {
- return getInstance(algorithm, provs[i]);
- }
- catch (NoSuchAlgorithmException nsae)
- {
- msg = nsae.getMessage();
- }
- }
- throw new NoSuchAlgorithmException(msg);
+ Provider[] p = Security.getProviders();
+ NoSuchAlgorithmException lastException = null;
+ for (int i = 0; i < p.length; i++)
+ try
+ {
+ return getInstance(algorithm, p[i]);
+ }
+ catch (NoSuchAlgorithmException x)
+ {
+ lastException = x;
+ }
+ if (lastException != null)
+ throw lastException;
+ throw new NoSuchAlgorithmException(algorithm);
}
/**
- * Get an instance of the named algorithm from the named provider.
- *
+ * Create an instance of the named algorithm from the named provider.
+ *
* @param algorithm The name of the algorithm.
- * @param provider The name of the provider.
+ * @param provider The name of the provider.
* @return An appropriate Mac instance, if the specified algorithm is
* implemented by the named provider.
- * @throws java.security.NoSuchAlgorithmException If the named provider
- * has no implementation of the algorithm.
- * @throws java.security.NoSuchProviderException If the named provider
- * does not exist.
+ * @throws NoSuchAlgorithmException If the named provider has no
+ * implementation of the algorithm.
+ * @throws NoSuchProviderException If the named provider does not exist.
+ * @throws IllegalArgumentException if either <code>algorithm</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>algorithm</code> is an empty string.
*/
public static final Mac getInstance(String algorithm, String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null)
+ throw new IllegalArgumentException("provider MUST NOT be null");
Provider p = Security.getProvider(provider);
if (p == null)
- {
- throw new NoSuchProviderException(provider);
- }
+ throw new NoSuchProviderException(provider);
return getInstance(algorithm, p);
}
/**
- * Get an instance of the named algorithm from a provider.
- *
+ * Create an instance of the named algorithm from a provider.
+ *
* @param algorithm The name of the algorithm.
- * @param provider The provider.
+ * @param provider The provider.
* @return An appropriate Mac instance, if the specified algorithm is
* implemented by the provider.
- * @throws java.security.NoSuchAlgorithmException If the provider
- * has no implementation of the algorithm.
+ * @throws NoSuchAlgorithmException If the provider has no implementation of
+ * the algorithm.
+ * @throws IllegalArgumentException if either <code>algorithm</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>algorithm</code> is an empty string.
*/
public static final Mac getInstance(String algorithm, Provider provider)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
+ StringBuilder sb = new StringBuilder("Mac algorithm [")
+ .append(algorithm).append("] from provider[")
+ .append(provider).append("] could not be created");
+ Throwable cause;
try
{
- return new Mac((MacSpi) Engine.getInstance(SERVICE, algorithm, provider),
- provider, algorithm);
+ Object spi = Engine.getInstance(SERVICE, algorithm, provider);
+ return new Mac((MacSpi) spi, provider, algorithm);
}
- catch (InvocationTargetException ite)
+ catch (InvocationTargetException x)
{
- if (ite.getCause() == null)
- throw new NoSuchAlgorithmException(algorithm);
- if (ite.getCause() instanceof NoSuchAlgorithmException)
- throw (NoSuchAlgorithmException) ite.getCause();
- throw new NoSuchAlgorithmException(algorithm);
+ cause = x.getCause();
+ if (cause instanceof NoSuchAlgorithmException)
+ throw (NoSuchAlgorithmException) cause;
+ if (cause == null)
+ cause = x;
}
- catch (ClassCastException cce)
+ catch (ClassCastException x)
{
- throw new NoSuchAlgorithmException(algorithm);
+ cause = x;
}
+ NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
+ x.initCause(cause);
+ throw x;
}
- // Instance methods.
- // ------------------------------------------------------------------------
-
/**
* Finishes the computation of a MAC and returns the digest.
*
@@ -398,6 +407,18 @@ public class Mac implements Cloneable
}
/**
+ * Update this MAC with the remaining bytes in the given buffer
+ * @param buffer The input buffer.
+ * @since 1.5
+ */
+ public final void update (final ByteBuffer buffer)
+ {
+ if (virgin)
+ throw new IllegalStateException ("not initialized");
+ macSpi.engineUpdate(buffer);
+ }
+
+ /**
* Clone this instance, if the underlying implementation supports it.
*
* @return A clone of this instance.
diff --git a/libjava/classpath/javax/crypto/MacSpi.java b/libjava/classpath/javax/crypto/MacSpi.java
index b0f96bf..853bd66 100644
--- a/libjava/classpath/javax/crypto/MacSpi.java
+++ b/libjava/classpath/javax/crypto/MacSpi.java
@@ -38,6 +38,7 @@ exception statement from your version. */
package javax.crypto;
+import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
@@ -142,4 +143,21 @@ public abstract class MacSpi
* @param length The number of bytes to update.
*/
protected abstract void engineUpdate(byte[] input, int offset, int length);
+
+ /**
+ * Update this MAC with the remaining bytes of a buffer.
+ *
+ * @param buffer The input buffer.
+ * @since 1.5
+ */
+ protected void engineUpdate (final ByteBuffer buffer)
+ {
+ byte[] buf = new byte[1024];
+ while (buffer.hasRemaining ())
+ {
+ int n = Math.min (buffer.remaining (), buf.length);
+ buffer.get (buf, 0, n);
+ engineUpdate (buf, 0, n);
+ }
+ }
}
diff --git a/libjava/classpath/javax/crypto/SecretKeyFactory.java b/libjava/classpath/javax/crypto/SecretKeyFactory.java
index 0a63ef0..1c857d2 100644
--- a/libjava/classpath/javax/crypto/SecretKeyFactory.java
+++ b/libjava/classpath/javax/crypto/SecretKeyFactory.java
@@ -94,94 +94,102 @@ public class SecretKeyFactory
this.algorithm = algorithm;
}
- // Class methods.
- // ------------------------------------------------------------------------
-
/**
- * Create a new secret key factory from the first appropriate
- * instance.
- *
+ * Create a new secret key factory from the first appropriate instance.
+ *
* @param algorithm The algorithm name.
* @return The appropriate key factory, if found.
- * @throws java.security.NoSuchAlgorithmException If no provider
- * implements the specified algorithm.
+ * @throws NoSuchAlgorithmException If no provider implements the specified
+ * algorithm.
+ * @throws IllegalArgumentException if <code>algorithm</code> is
+ * <code>null</code> or is an empty string.
*/
public static final SecretKeyFactory getInstance(String algorithm)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
- Provider[] provs = Security.getProviders();
- for (int i = 0; i < provs.length; i++)
- {
- try
- {
- return getInstance(algorithm, provs[i]);
- }
- catch (NoSuchAlgorithmException nsae)
- {
- }
- }
- throw new NoSuchAlgorithmException(algorithm);
+ Provider[] p = Security.getProviders();
+ NoSuchAlgorithmException lastException = null;
+ for (int i = 0; i < p.length; i++)
+ try
+ {
+ return getInstance(algorithm, p[i]);
+ }
+ catch (NoSuchAlgorithmException x)
+ {
+ lastException = x;
+ }
+ if (lastException != null)
+ throw lastException;
+ throw new NoSuchAlgorithmException(algorithm);
}
/**
* Create a new secret key factory from the named provider.
- *
+ *
* @param algorithm The algorithm name.
- * @param provider The provider name.
+ * @param provider The provider name.
* @return The appropriate key factory, if found.
- * @throws java.security.NoSuchAlgorithmException If the named
- * provider does not implement the algorithm.
- * @throws java.security.NoSuchProviderException If the named provider
- * does not exist.
+ * @throws NoSuchAlgorithmException If the named provider does not implement
+ * the algorithm.
+ * @throws NoSuchProviderException If the named provider does not exist.
+ * @throws IllegalArgumentException if either <code>algorithm</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>algorithm</code> is an empty string.
*/
public static final SecretKeyFactory getInstance(String algorithm,
String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null)
+ throw new IllegalArgumentException("provider MUST NOT be null");
Provider p = Security.getProvider(provider);
if (p == null)
- {
- throw new NoSuchProviderException(provider);
- }
+ throw new NoSuchProviderException(provider);
return getInstance(algorithm, p);
}
/**
* Create a new secret key factory from the specified provider.
- *
+ *
* @param algorithm The algorithm name.
- * @param provider The provider.
+ * @param provider The provider.
* @return The appropriate key factory, if found.
- * @throws java.security.NoSuchAlgorithmException If the provider
- * does not implement the algorithm.
+ * @throws NoSuchAlgorithmException If the provider does not implement the
+ * algorithm.
+ * @throws IllegalArgumentException if either <code>algorithm</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>algorithm</code> is an empty string.
*/
public static final SecretKeyFactory getInstance(String algorithm,
Provider provider)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
+ StringBuilder sb = new StringBuilder("SecretKeyFactory algorithm [")
+ .append(algorithm).append("] from provider[")
+ .append(provider).append("] could not be created");
+ Throwable cause;
try
{
- return new SecretKeyFactory((SecretKeyFactorySpi)
- Engine.getInstance(SERVICE, algorithm, provider),
- provider, algorithm);
+ Object spi = Engine.getInstance(SERVICE, algorithm, provider);
+ return new SecretKeyFactory((SecretKeyFactorySpi) spi, provider, algorithm);
}
- catch (InvocationTargetException ite)
+ catch (InvocationTargetException x)
{
- if (ite.getCause() == null)
- throw new NoSuchAlgorithmException(algorithm);
- if (ite.getCause() instanceof NoSuchAlgorithmException)
- throw (NoSuchAlgorithmException) ite.getCause();
- throw new NoSuchAlgorithmException(algorithm);
+ cause = x.getCause();
+ if (cause instanceof NoSuchAlgorithmException)
+ throw (NoSuchAlgorithmException) cause;
+ if (cause == null)
+ cause = x;
}
- catch (ClassCastException cce)
+ catch (ClassCastException x)
{
- throw new NoSuchAlgorithmException(algorithm);
+ cause = x;
}
+ NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
+ x.initCause(cause);
+ throw x;
}
- // Instance methods.
- // ------------------------------------------------------------------------
-
/**
* Generate a secret key from a key specification, if possible.
*