diff options
author | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
---|---|---|
committer | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
commit | 554fd8c5195424bdbcabf5de30fdc183aba391bd (patch) | |
tree | 976dc5ab7fddf506dadce60ae936f43f58787092 /libjava/classpath/javax/crypto/spec | |
download | cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2 cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz |
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig;
imported gcc-4.6.4 source tree from verified upstream tarball.
downloading a git-generated archive based on the 'upstream' tag
should provide you with a source tree that is binary identical
to the one extracted from the above tarball.
if you have obtained the source via the command 'git clone',
however, do note that line-endings of files in your working
directory might differ from line-endings of the respective
files in the upstream repository.
Diffstat (limited to 'libjava/classpath/javax/crypto/spec')
-rw-r--r-- | libjava/classpath/javax/crypto/spec/DESKeySpec.java | 220 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/DESedeKeySpec.java | 151 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/DHGenParameterSpec.java | 100 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/DHParameterSpec.java | 135 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/DHPrivateKeySpec.java | 115 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/DHPublicKeySpec.java | 115 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/IvParameterSpec.java | 96 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/PBEKeySpec.java | 281 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/PBEParameterSpec.java | 100 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/RC2ParameterSpec.java | 166 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/RC5ParameterSpec.java | 202 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/SecretKeySpec.java | 163 | ||||
-rw-r--r-- | libjava/classpath/javax/crypto/spec/package.html | 46 |
13 files changed, 1890 insertions, 0 deletions
diff --git a/libjava/classpath/javax/crypto/spec/DESKeySpec.java b/libjava/classpath/javax/crypto/spec/DESKeySpec.java new file mode 100644 index 000000000..9075a77d2 --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/DESKeySpec.java @@ -0,0 +1,220 @@ +/* DESKeySpec -- Keys for DES. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.security.InvalidKeyException; +import java.security.spec.KeySpec; + +/** + * This class is a transparent wrapper for DES keys, which are arrays + * of 8 bytes. + * + * @author Casey Marshall (csm@gnu.org) + * @since 1.4 + */ +public class DESKeySpec implements KeySpec +{ + + // Constants. + // ------------------------------------------------------------------------ + + /** + * The length of a DES key, in bytes. + */ + public static final int DES_KEY_LEN = 8; + + /** + * The key bytes. + */ + private byte[] key; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new DES key spec, copying the first 8 bytes from the + * byte array. + * + * @param key The key bytes. + * @throws java.security.InvalidKeyException If there are less than 8 + * bytes in the array. + */ + public DESKeySpec(byte[] key) throws InvalidKeyException + { + this(key, 0); + } + + /** + * Create a new DES key spec, starting at <code>offset</code> in + * the byte array. The first 8 bytes starting at <code>offset</code> + * are copied. + * + * @param key The key bytes. + * @param offset The offset into the byte array at which to begin. + * @throws java.security.InvalidKeyException If there are less than 8 + * bytes starting at <code>offset</code>. + */ + public DESKeySpec(byte[] key, int offset) throws InvalidKeyException + { + if (key.length - offset < DES_KEY_LEN) + { + throw new InvalidKeyException("DES keys must be 8 bytes long"); + } + this.key = new byte[DES_KEY_LEN]; + System.arraycopy(key, offset, this.key, 0, DES_KEY_LEN); + } + + // Class methods. + // ------------------------------------------------------------------------ + + /** + * Returns whether or not the given key is <i>parity adjusted</i>; + * i.e. every byte in the key has an odd number of "1" bits. + * + * @param key The key bytes, considered between <code>[offset, + * offset+7]</code> + * @param offset The offset into the byte array at which to begin. + * @return True if all bytes have an odd number of "1" bits. + * @throws java.security.InvalidKeyException If there are not enough + * bytes in the array. + */ + public static boolean isParityAdjusted(byte[] key, int offset) + throws InvalidKeyException + { + if (key.length - offset < DES_KEY_LEN) + { + throw new InvalidKeyException("DES keys must be 8 bytes long"); + } + boolean parity = false; + boolean oddbits = false; + for (int i = 0; i < DES_KEY_LEN; i++) + { + oddbits = false; + for (int j = 0; j < 8; j++) + { + oddbits ^= (key[i+offset] & 1 << j) != 0; + } + parity &= oddbits; + } + return parity; + } + + /** + * One-half of the weak and semiweak DES keys (the other half are the + * complements of these). + */ + private static final byte[][] WEAK_KEYS = new byte[][] { + { 0, 0, 0, 0, 0, 0, 0, 0 }, // 0000 0000 0000 0000 + { -1, -1, -1, -1, 0, 0, 0, 0 }, // ffff ffff 0000 0000 + { 1, 1, 1, 1, 1, 1, 1, 1 }, // 0101 0101 0101 0101 + { 31, 31, 31, 31, 14, 14, 14, 14 }, // 1f1f 1f1f 0e0e 0e0e + { 1, -2, 1, -2, 1, -2, 1, -2 }, // 01fe 01fe 01fe 01fe + { 31, -32, 31, -32, -32, 31, -32, 31 }, // 1fe0 1fe0 0e1f 0e1f + { 1, -32, 1, -32, 1, -15, 1, -15 }, // 01e0 01e0 01f1 01f1 + { 31, -2, 31, -2, 14, -2, 14, -2 }, // 1ffe 1ffe 0efe 0efe + { 1, 31, 1, 31, 1, 14, 1, 14 }, // 011f 011f 010e 010e + { -32, -2, -32, -2, -15, -2, -15, -2 }, // e0fe e0fe f1fe f1fe + }; + + /** + * Tests if the bytes between <code>[offset, offset+7]</code> + * constitute a weak or semi-weak DES key. + * + * @param key The key bytes to check. + * @param offset The offset in the byte array to start. + * @return true If the key bytes are a weak key. + */ + public static boolean isWeak(byte[] key, int offset) + throws InvalidKeyException + { + if (key.length - offset < DES_KEY_LEN) + { + throw new InvalidKeyException("DES keys must be 8 bytes long"); + } + for (int i = 0; i < WEAK_KEYS.length; i++) + { + if (equalsOrComplementEquals(key, offset, WEAK_KEYS[i])) + { + return true; + } + } + return false; + } + + /** + * This method returns true if the first 8 bytes starting at + * <code>off</code> in <code>a</code> equal the first 8 bytes in + * <code>b</code>, or equal the <i>complement</i> of the first 8 bytes + * in <code>b</code>. + * + * @param a The first byte array. + * @param off The index into the first byte array. + * @param b The second byte array. + * @return <code>a == b || a == ~b</code> + */ + private static boolean equalsOrComplementEquals(byte[] a, int off, byte[] b) + { + boolean result = true; + for (int i = 0; i < DES_KEY_LEN; i++) + { + result &= a[off+i] == b[i]; + } + if (result) return true; + result = true; + for (int i = 0; i < DES_KEY_LEN; i++) + { + result &= a[off+i] == (~b[i]); + } + return result; + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Return the key as a byte array. This method does not copy the byte + * array. + * + * @return The key bytes. + */ + public byte[] getKey() + { + return key; + } +} diff --git a/libjava/classpath/javax/crypto/spec/DESedeKeySpec.java b/libjava/classpath/javax/crypto/spec/DESedeKeySpec.java new file mode 100644 index 000000000..1f6a25026 --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/DESedeKeySpec.java @@ -0,0 +1,151 @@ +/* DESedeKeySpec.java -- Keys for triple-DES. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.security.InvalidKeyException; +import java.security.spec.KeySpec; + +/** + * This class is a transparent wrapper for DES-EDE (Triple-DES) keys, + * which are arrays of 24 bytes. + * + * @author Casey Marshall (csm@gnu.org) + * @since 1.4 + */ +public class DESedeKeySpec implements KeySpec +{ + + // Constants. + // ------------------------------------------------------------------------ + + /** + * The length of a triple-DES key, in bytes. + */ + public static final int DES_EDE_KEY_LEN = 24; + + /** + * The key bytes. + */ + private byte[] key; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new DES-EDE key spec, copying the first 24 bytes from the + * byte array. + * + * @param key The key bytes. + * @throws java.security.InvalidKeyException If there are less than 24 + * bytes in the array. + */ + public DESedeKeySpec(byte[] key) throws InvalidKeyException + { + this(key, 0); + } + + /** + * Create a new DES-EDE key spec, starting at <code>offset</code> in + * the byte array. The first 24 bytes starting at <code>offset</code> + * are copied. + * + * @param key The key bytes. + * @param offset The offset into the byte array at which to begin. + * @throws java.security.InvalidKeyException If there are less than 24 + * bytes starting at <code>offset</code>. + */ + public DESedeKeySpec(byte[] key, int offset) throws InvalidKeyException + { + if (key.length - offset < DES_EDE_KEY_LEN) + { + throw new InvalidKeyException("DES-EDE keys must be 24 bytes long"); + } + this.key = new byte[DES_EDE_KEY_LEN]; + System.arraycopy(key, offset, this.key, 0, DES_EDE_KEY_LEN); + } + + // Class methods. + // ------------------------------------------------------------------------ + + /** + * Returns whether or not the given key is <i>parity adjusted</i>; + * i.e. every byte in the key has an odd number of "1" bits. + * + * @param key The key bytes, considered between <code>[offset, + * offset+23]</code> + * @param offset The offset into the byte array at which to begin. + * @return True if all bytes have an odd number of "1" bits. + * @throws java.security.InvalidKeyException If there are not enough + * bytes in the array. + */ + public static boolean isParityAdjusted(byte[] key, int offset) + throws InvalidKeyException + { + if (key.length - offset < DES_EDE_KEY_LEN) + { + throw new InvalidKeyException("DES-EDE keys must be 24 bytes long"); + } + boolean parity = false; + boolean oddbits = false; + for (int i = 0; i < DES_EDE_KEY_LEN; i++) + { + oddbits = false; + for (int j = 0; j < 8; j++) + { + oddbits ^= (key[i+offset] & 1 << j) != 0; + } + parity &= oddbits; + } + return parity; + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Return the key as a byte array. This method does not copy the byte + * array. + * + * @return The key bytes. + */ + public byte[] getKey() + { + return key; + } +} diff --git a/libjava/classpath/javax/crypto/spec/DHGenParameterSpec.java b/libjava/classpath/javax/crypto/spec/DHGenParameterSpec.java new file mode 100644 index 000000000..0fc8ed58e --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/DHGenParameterSpec.java @@ -0,0 +1,100 @@ +/* DHGenParameterSpec.java -- Diffie-Hellman parameter generator spec. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.security.spec.AlgorithmParameterSpec; + +/** + * This class represents the parameters needed for generating + * Diffie-Hellman parameters. + * + * @author Casey Marshall (csm@gnu.org) + * @since 1.4 + * @see DHParameterSpec + */ +public class DHGenParameterSpec implements AlgorithmParameterSpec +{ + + // Variables. + // ------------------------------------------------------------------------ + + /** The length of the prime, in bits. */ + private int primeSize; + + /** The length of the exponent, in bits. */ + private int exponentSize; + + // Constructor. + // ------------------------------------------------------------------------ + + /** + * Create a new Diffie-Hellman parameter generator spec. + * + * @param primeSize The size of the prime, in bits. + * @param exponentSize The size of the exponent, in bits. + */ + public DHGenParameterSpec(int primeSize, int exponentSize) + { + this.primeSize = primeSize; + this.exponentSize = exponentSize; + } + + // Intance methods. + // ------------------------------------------------------------------------ + + /** + * Get the size of the exponent, in bits. + * + * @return The exponent size. + */ + public int getExponentSize() + { + return exponentSize; + } + + /** + * Get the size of the prime, in bits. + * + * @return The prime size. + */ + public int getPrimeSize() + { + return primeSize; + } +} diff --git a/libjava/classpath/javax/crypto/spec/DHParameterSpec.java b/libjava/classpath/javax/crypto/spec/DHParameterSpec.java new file mode 100644 index 000000000..4db82870f --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/DHParameterSpec.java @@ -0,0 +1,135 @@ +/* DHParameterSpec.java -- Parameters for Diffie-Hellman keys. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.math.BigInteger; +import java.security.spec.AlgorithmParameterSpec; + +/** + * The base set of parameters necessary to perform Diffie-Hellman key + * exchange. Each party in the key exchange shares these parameters. + * + * <p>Each set of parameters consists of a <i>base generator</i> + * <code>g</code>, a <i>prime modulus</i> <code>p</code>, and an + * optional length, in bits, of the private exponent. + * + * <p>See <a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-3/">PKCS + * #3 - Diffie-Hellman Key Agreement Standard</a> for more information. + * + * @author Casey Marshall (csm@gnu.org) + * @since 1.4 + * @see javax.crypto.KeyAgreement + */ +public class DHParameterSpec implements AlgorithmParameterSpec +{ + + // Variables. + // ------------------------------------------------------------------------ + + /** The base generator g. */ + private BigInteger g; + + /** The prime modulus p. */ + private BigInteger p; + + /** The length, in bits, of the private exponent. */ + private int l; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new set of Diffie-Hellman parameters. + * + * @param p The prime modulus. + * @param g The base generator. + */ + public DHParameterSpec(BigInteger p, BigInteger g) + { + this(p, g, 0); + } + + /** + * Create a new set of Diffie-Hellman parameters. + * + * @param p The prime modulus. + * @param g The base generator. + * @param l The size of the private exponent, in bits. + */ + public DHParameterSpec(BigInteger p, BigInteger g, int l) + { + this.p = p; + this.g = g; + this.l = l; + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Get the base generator, <i>g</i>. + * + * @return The base generator <i>g</i>. + */ + public BigInteger getG() + { + return g; + } + + /** + * Get the length of the private exponent, in bits. + * + * @return The length of the private exponent, in bits, or 0 if this + * has not been explicitly set. + */ + public int getL() + { + return l; + } + + /** + * Get the prime modulus, <i>p</i>. + * + * @return The prime modulus, <i>p</i>. + */ + public BigInteger getP() + { + return p; + } +} diff --git a/libjava/classpath/javax/crypto/spec/DHPrivateKeySpec.java b/libjava/classpath/javax/crypto/spec/DHPrivateKeySpec.java new file mode 100644 index 000000000..348a19264 --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/DHPrivateKeySpec.java @@ -0,0 +1,115 @@ +/* DHPrivateKeySpec.java -- Wrapper for Diffie-Hellman private keys. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.math.BigInteger; +import java.security.spec.KeySpec; + +/** + * A wrapper for Diffie-Hellman private key data. + * + * @author Casey Marshall (csm@gnu.org) + * @since 1.4 + * @see DHPublicKeySpec + */ +public class DHPrivateKeySpec implements KeySpec +{ + + // Variables. + // ------------------------------------------------------------------------ + + /** The base generator. */ + private BigInteger g; + + /** The prime modulus. */ + private BigInteger p; + + /** The private exponent. */ + private BigInteger x; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new Diffie-Hellman private key spec. + * + * @param x The private exponent. + * @param p The prime modulus. + * @param g The base generator. + */ + public DHPrivateKeySpec(BigInteger x, BigInteger p, BigInteger g) + { + this.x = x; + this.p = p; + this.g = g; + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Get the base generator. + * + * @return The base generator. + */ + public BigInteger getG() + { + return g; + } + + /** + * Get the prime modulus. + * + * @return The prime modulus. + */ + public BigInteger getP() + { + return p; + } + + /** + * Get the private exponent. + * + * @return The private exponent. + */ + public BigInteger getX() + { + return x; + } +} diff --git a/libjava/classpath/javax/crypto/spec/DHPublicKeySpec.java b/libjava/classpath/javax/crypto/spec/DHPublicKeySpec.java new file mode 100644 index 000000000..e81872785 --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/DHPublicKeySpec.java @@ -0,0 +1,115 @@ +/* DHPublicKeySpec.java -- Wrapper for Diffie-Hellman public keys. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.math.BigInteger; +import java.security.spec.KeySpec; + +/** + * A wrapper for Diffie-Hellman public key data. + * + * @author Casey Marshall (csm@gnu.org) + * @since 1.4 + * @see DHPrivateKeySpec + */ +public class DHPublicKeySpec implements KeySpec +{ + + // Variables. + // ------------------------------------------------------------------------ + + /** The base generator. */ + private BigInteger g; + + /** The prime modulus. */ + private BigInteger p; + + /** The public value. */ + private BigInteger y; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new Diffie-Hellman public key spec. + * + * @param y The public value. + * @param p The prime modulus. + * @param g The base generator. + */ + public DHPublicKeySpec(BigInteger y, BigInteger p, BigInteger g) + { + this.y = y; + this.p = p; + this.g = g; + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Get the base generator. + * + * @return The base generator. + */ + public BigInteger getG() + { + return g; + } + + /** + * Get the prime modulus. + * + * @return The prime modulus. + */ + public BigInteger getP() + { + return p; + } + + /** + * Get the public value. + * + * @return The public value. + */ + public BigInteger getY() + { + return y; + } +} diff --git a/libjava/classpath/javax/crypto/spec/IvParameterSpec.java b/libjava/classpath/javax/crypto/spec/IvParameterSpec.java new file mode 100644 index 000000000..3af38f58e --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/IvParameterSpec.java @@ -0,0 +1,96 @@ +/* IvParameterSpec.java -- A simple wrapper for initialization vectors. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.security.spec.AlgorithmParameterSpec; + +/** + * A wrapper for an initialization vector. An initialization vector is + * necessary for any cipher in any <i>feedback mode</i>, e.g. CBC. + * + * @author Casey Marshall (csm@gnu.org) + */ +public class IvParameterSpec implements AlgorithmParameterSpec +{ + + // Fields. + // ------------------------------------------------------------------------ + + /** The IV. */ + private byte[] iv; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new initialization vector spec from an entire byte array. + * + * @param iv The IV bytes. + */ + public IvParameterSpec(byte[] iv) + { + this(iv, 0, iv.length); + } + + /** + * Create a new initialization vector spec from part of a byte array. + * + * @param iv The IV bytes. + * @param off The offset into the IV bytes. + * @param len The number of IV bytes. + */ + public IvParameterSpec(byte[] iv, int off, int len) + { + this.iv = new byte[len]; + System.arraycopy(iv, off, this.iv, 0, len); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Returns the IV. This method does not copy the byte array. + * + * @return The IV. + */ + public byte[] getIV() + { + return iv; + } +} diff --git a/libjava/classpath/javax/crypto/spec/PBEKeySpec.java b/libjava/classpath/javax/crypto/spec/PBEKeySpec.java new file mode 100644 index 000000000..f0ffa379a --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/PBEKeySpec.java @@ -0,0 +1,281 @@ +/* PBEKeySpec.java -- Wrapper for password-based keys. + Copyright (C) 2004, 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.security.spec.KeySpec; + +/** + * A wrapper for a password-based key, used for password-based + * encryption (PBE). + * + * <p>Examples of password-based encryption algorithms include: + * + * <ul> + * <li><a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/">PKCS #5 + * - Password-Based Cryptography Standard</a></li> + * <li><a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-12/">PKCS + * #12 - Personal Information Exchange Syntax Standard</a></li> + * </ul> + * + * @author Casey Marshall (csm@gnu.org) + * @since 1.4 + * @see javax.crypto.SecretKeyFactory + * @see PBEParameterSpec + */ +public class PBEKeySpec implements KeySpec +{ + + // Fields. + // ------------------------------------------------------------------------ + + /** The iteration count. */ + private int iterationCount; + + /** The generated key length. */ + private int keyLength; + + /** The password. */ + private char[] password; + + /** The salt. */ + private byte[] salt; + + /** The password state */ + private boolean passwordValid = true; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new PBE key spec with just a password. + * <p> + * A copy of the password argument is stored instead of the argument itself. + * + * @param password The password char array. + */ + public PBEKeySpec(char[] password) + { + setPassword(password); + + // load the default values for unspecified variables. + salt = null; + iterationCount = 0; + keyLength = 0; + } + + /** + * Create a PBE key spec with a password, salt, and iteration count. + * <p> + * A copy of the password and salt arguments are stored instead of the + * arguments themselves. + * + * @param password The password char array. + * @param salt The salt bytes. + * @param iterationCount The iteration count. + * @throws NullPointerException If salt is null + * @throws IllegalArgumentException If salt is an empty array, or + * iterationCount is negative + */ + public PBEKeySpec(char[] password, byte[] salt, int iterationCount) + { + setPassword(password); + setSalt(salt); + setIterationCount(iterationCount); + + // load default values into unspecified variables. + keyLength = 0; + } + + /** + * Create a PBE key spec with a password, salt, iteration count, and key + * length. + * <p> + * A copy of the password and salt arguments are stored instead of the + * arguments themselves. + * + * @param password The password char array. + * @param salt The salt bytes. + * @param iterationCount The iteration count. + * @param keyLength The generated key length. + * @throws NullPointerException If salt is null + * @throws IllegalArgumentException If salt is an empty array, if + * iterationCount or keyLength is negative + */ + public PBEKeySpec(char[] password, byte[] salt, int iterationCount, + int keyLength) + { + setPassword(password); + setSalt(salt); + setIterationCount(iterationCount); + setKeyLength(keyLength); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Clear the password array by filling it with null characters. + * <p> + * This clears the stored copy of the password, not the original char array + * used to create the password. + */ + public final void clearPassword() + { + if (password == null) + return; + for (int i = 0; i < password.length; i++) + password[i] = '\u0000'; + + // since the password is cleared, it is no longer valid + passwordValid = false; + } + + /** + * Get the iteration count, or 0 if it has not been specified. + * + * @return The iteration count, or 0 if it has not been specified. + */ + public final int getIterationCount() + { + return iterationCount; + } + + /** + * Get the generated key length, or 0 if it has not been specified. + * + * @return The key length, or 0 if it has not been specified. + */ + public final int getKeyLength() + { + return keyLength; + } + + /** + * Get the password character array copy. + * <p> + * This returns a copy of the password, not the password itself. + * + * @return a clone of the password. + * @throws IllegalStateException If {@link #clearPassword()} has already been + * called. + */ + public final char[] getPassword() + { + if (! passwordValid) + throw new IllegalStateException("clearPassword() has been called, the " + + "password is no longer valid"); + return (char[]) password.clone(); + } + + /** + * Get the salt bytes array copy. + * <p> + * This returns a copy of the salt, not the salt itself. + * + * @return The salt. + */ + public final byte[] getSalt() + { + if (salt != null) + return (byte[]) salt.clone(); + return null; + } + + /** + * Set the password char array. + * <p> + * A copy of the password argument is stored instead of the argument itself. + * + * @param password The password to be set + */ + private void setPassword(char[] password) + { + if (password != null) + this.password = (char[]) password.clone(); + else + this.password = new char[0]; + + passwordValid = true; + } + + /** + * Set the salt byte array. + * <p> + * A copy of the salt arguments is stored instead of the argument itself. + * + * @param salt The salt to be set. + * @throws NullPointerException If the salt is null. + * @throws IllegalArgumentException If the salt is an empty array. + */ + private void setSalt(byte[] salt) + { + if (salt.length == 0) + throw new IllegalArgumentException("salt MUST NOT be an empty byte array"); + + this.salt = (byte[]) salt.clone(); + } + + /** + * Set the iterationCount. + * + * @param iterationCount The iteration count to be set. + * @throws IllegalArgumentException If the iterationCount is negative. + */ + private void setIterationCount(int iterationCount) + { + if (iterationCount < 0) + throw new IllegalArgumentException("iterationCount MUST be positive"); + + this.iterationCount = iterationCount; + } + + /** + * Set the keyLength. + * + * @param keyLength The keyLength to be set. + * @throws IllegalArgumentException if the keyLength is negative. + */ + private void setKeyLength(int keyLength) + { + if (keyLength < 0) + throw new IllegalArgumentException("keyLength MUST be positive"); + + this.keyLength = keyLength; + } +} diff --git a/libjava/classpath/javax/crypto/spec/PBEParameterSpec.java b/libjava/classpath/javax/crypto/spec/PBEParameterSpec.java new file mode 100644 index 000000000..322d9556c --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/PBEParameterSpec.java @@ -0,0 +1,100 @@ +/* PBEParameterSpec.java -- A wrapper for PBE parameters. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.security.spec.AlgorithmParameterSpec; + +/** + * A wrapper for the parameters used in <a + * href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/">PKCS #5 - + * Password-Based Cryptography Standard</a>. + * + * @author Casey Marshall (csm@gnu.org) + * @since 1.4 + */ +public class PBEParameterSpec implements AlgorithmParameterSpec +{ + + // Fields. + // ------------------------------------------------------------------------ + + /** The iteration count. */ + private int iterationCount; + + /** The salt. */ + private byte[] salt; + + // Constructor. + // ------------------------------------------------------------------------ + + /** + * Creates a new password-based encryption parameter specification. + * + * @param salt The salt. + * @param iterationCount The iteration count. + */ + public PBEParameterSpec(byte[] salt, int iterationCount) + { + this.salt = salt; + this.iterationCount = iterationCount; + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Get the iteration count. + * + * @return The iteration count. + */ + public int getIterationCount() + { + return iterationCount; + } + + /** + * Get the salt. + * + * @return The salt. + */ + public byte[] getSalt() + { + return salt; + } +} diff --git a/libjava/classpath/javax/crypto/spec/RC2ParameterSpec.java b/libjava/classpath/javax/crypto/spec/RC2ParameterSpec.java new file mode 100644 index 000000000..33155b2b3 --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/RC2ParameterSpec.java @@ -0,0 +1,166 @@ +/* RC2ParameterSpec.java -- Wrapper for RC2 parameters. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.security.spec.AlgorithmParameterSpec; + +/** + * A wrapper for parameters for the <a + * href="http://www.rsasecurity.com/rsalabs/faq/3-6-2.html">RC2</a> + * block cipher ("RC" means either "Rivest Cipher" or "Ron's Code", + * depending upon who you ask and when). + * + * @author Casey Marshall (csm@gnu.org) + * @since 1.4 + */ +public class RC2ParameterSpec implements AlgorithmParameterSpec +{ + + // Constants and fields. + // ------------------------------------------------------------------------ + + /** The length of an RC2 IV, in bytes. */ + private static final int RC2_IV_LENGTH = 8; + + /** The effective key length, in bits. */ + private int effectiveKeyBits; + + /** The initialization vector. */ + private byte[] iv; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create RC2 parameters without an IV. + * + * @param effectiveKeyBits The number of effective key bits. + */ + public RC2ParameterSpec(int effectiveKeyBits) + { + this.effectiveKeyBits = effectiveKeyBits; + } + + /** + * Create RC2 parameters with an IV. + * + * @param effectiveKeyBits The number of effective key bits. + * @param iv The IV; the first eight bytes of this array + * are used. + */ + public RC2ParameterSpec(int effectiveKeyBits, byte[] iv) + { + this(effectiveKeyBits, iv, 0); + } + + /** + * Create RC2 parameters with an IV. + * + * @param effectiveKeyBits The number of effective key bits. + * @param iv The IV; the first eight bytes of this array + * after <code>offset</code> are used. + * @param offset From whence to start in the array. + */ + public RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset) + { + if (iv.length - offset < RC2_IV_LENGTH) + { + throw new IllegalArgumentException("IV too short"); + } + this.effectiveKeyBits = effectiveKeyBits; + this.iv = new byte[RC2_IV_LENGTH]; + System.arraycopy(iv, offset, this.iv, 0, RC2_IV_LENGTH); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Get the number of effective key bits. + * + * @return The numer of effective key bits. + */ + public int getEffectiveKeyBits() + { + return effectiveKeyBits; + } + + /** + * Return the initialization vector, or <code>null</code> if none was + * specified. + * + * @return The IV, or null. + */ + public byte[] getIV() + { + return iv; + } + + public boolean equals(Object o) + { + if (this == o) return true; + byte[] oiv = ((RC2ParameterSpec) o).getIV(); + if (iv != oiv) + { + if (iv == null || oiv == null) return false; + if (iv.length != oiv.length) return false; + for (int i = 0; i < iv.length; i++) + { + if (iv[i] != oiv[i]) + { + return false; + } + } + } + return effectiveKeyBits == ((RC2ParameterSpec) o).getEffectiveKeyBits(); + } + + public int hashCode() + { + int code = effectiveKeyBits; + if (iv != null) + { + for (int i = 0; i < RC2_IV_LENGTH; i++) + { + code += iv[i]; + } + } + return code; + } +} diff --git a/libjava/classpath/javax/crypto/spec/RC5ParameterSpec.java b/libjava/classpath/javax/crypto/spec/RC5ParameterSpec.java new file mode 100644 index 000000000..8570c86b3 --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/RC5ParameterSpec.java @@ -0,0 +1,202 @@ +/* RC5ParameterSpec.java -- parameters for RC5. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.security.spec.AlgorithmParameterSpec; + +/** + * A wrapper for parameters to the <a + * href="http://www.rsasecurity.com/rsalabs/faq/3-6-4.html">RC5</a> + * block cipher. + * + * @author Casey Marshall (csm@gnu.org) + * @since 1.4 + */ +public class RC5ParameterSpec implements AlgorithmParameterSpec +{ + + // Fields. + // ------------------------------------------------------------------------ + + /** The IV. */ + private byte[] iv; + + /** The number of rounds. */ + private int rounds; + + /** The version number. */ + private int version; + + /** The word size, in bits. */ + private int wordSize; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create RC5 parameters without an IV. + * + * @param version The version number. + * @param rounds The number of rounds. + * @param wordSize The size of a word, in bits. + */ + public RC5ParameterSpec(int version, int rounds, int wordSize) + { + this.version = version; + this.rounds = rounds; + this.wordSize = wordSize; + } + + /** + * Create RC5 parameters with an IV. The bytes in <code>iv</code> in + * the range <code>[0, 2*(wordSize/8)-1]</code> are used. + * + * @param version The version number. + * @param rounds The number of rounds. + * @param wordSize The size of a word, in bits. + * @param iv The IV data. + */ + public RC5ParameterSpec(int version, int rounds, int wordSize, byte[] iv) + { + this(version, rounds, wordSize, iv, 0); + } + + /** + * Create RC5 parameters with an IV. The bytes in <code>iv</code> in + * the range <code>[off, off+2*(wordSize/8)-1]</code> are used. + * + * @param version The version number. + * @param rounds The number of rounds. + * @param wordSize The size of a word, in bits. + * @param iv The IV data. + * @param off From where in the array the IV starts. + */ + public + RC5ParameterSpec(int version, int rounds, int wordSize, byte[] iv, int off) + { + this(version, rounds, wordSize); + int ivLength = 2 * (wordSize / 8); + if (off < 0) + throw new IllegalArgumentException(); + if (iv.length - off < ivLength) + { + throw new IllegalArgumentException("IV too short"); + } + this.iv = new byte[ivLength]; + System.arraycopy(iv, off, this.iv, 0, ivLength); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Return the initializaiton vector, or <code>null</code> if none was + * specified. + * + * @return The IV, or null. + */ + public byte[] getIV() + { + return iv; + } + + /** + * Get the number of rounds. + * + * @return The number of rounds. + */ + public int getRounds() + { + return rounds; + } + + /** + * Get the version number. + * + * @return The version number. + */ + public int getVersion() + { + return version; + } + + /** + * Get the word size, in bits. + * + * @return The word size, in bits. + */ + public int getWordSize() + { + return wordSize; + } + + public boolean equals(Object o) + { + if (this == o) return true; + byte[] oiv = ((RC5ParameterSpec) o).getIV(); + if (iv != oiv) + { + if (iv == null || oiv == null) return false; + if (iv.length != oiv.length) return false; + for (int i = 0; i < iv.length; i++) + { + if (iv[i] != oiv[i]) + { + return false; + } + } + } + return rounds == ((RC5ParameterSpec) o).getRounds() + && version == ((RC5ParameterSpec) o).getVersion() + && wordSize == ((RC5ParameterSpec) o).getWordSize(); + } + + public int hashCode() + { + int code = rounds + version + wordSize; + if (iv != null) + { + for (int i = 0; i < iv.length; i++) + { + code += iv[i]; + } + } + return code; + } +} diff --git a/libjava/classpath/javax/crypto/spec/SecretKeySpec.java b/libjava/classpath/javax/crypto/spec/SecretKeySpec.java new file mode 100644 index 000000000..86c4e05d4 --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/SecretKeySpec.java @@ -0,0 +1,163 @@ +/* SecretKeySpec.java -- Wrapper for secret keys. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.crypto.spec; + +import java.security.spec.KeySpec; + +import javax.crypto.SecretKey; + +/** + * This is a simple wrapper around a raw byte array, for ciphers that do + * not require any key parameters other than the bytes themselves. + * + * <p>Since this class implements {@link javax.crypto.SecretKey}, which + * in turn extends {@link java.security.Key}, so instances of this class + * may be passed directly to the <code>init()</code> methods of {@link + * javax.crypto.Cipher}. + * + * @see javax.crypto.SecretKey + * @see javax.crypto.SecretKeyFactory + */ +public class SecretKeySpec implements KeySpec, SecretKey +{ + + // Constants and fields. + // ------------------------------------------------------------------------ + + /** Compatible with JDK1.4. */ + private static final long serialVersionUID = 6577238317307289933L; + + /** The key bytes. */ + private byte[] key; + + /** The algorithm's name. */ + private String algorithm; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new secret key spec from an entire byte array. + * + * @param key The key material. + * @param algorithm The name of the algorithm using this key. + */ + public SecretKeySpec(byte[] key, String algorithm) + { + this(key, 0, key.length, algorithm); + } + + /** + * Create a new secret key spec from part of a byte array. + * + * @param key The key material. + * @param off The offset at which key material begins. + * @param len The length of key material. + * @param algorithm The name of the algorithm using this key. + */ + public SecretKeySpec(byte[] key, int off, int len, String algorithm) + { + this.key = new byte[len]; + this.algorithm = algorithm; + System.arraycopy(key, off, this.key, 0, len); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Return the name of the algorithm associated with this secret key. + * + * @return The algorithm's name. + */ + public String getAlgorithm() + { + return algorithm; + } + + /** + * Return the key as a byte array. + * + * @return The key material. + */ + public byte[] getEncoded() + { + return key; + } + + /** + * This key's format, which is always "RAW". + * + * @return "RAW" + */ + public String getFormat() + { + return "RAW"; + } + + public boolean equals(Object o) + { + if (o instanceof SecretKeySpec) + { + byte[] okey = ((SecretKeySpec) o).getEncoded(); + if (key.length != okey.length) + return false; + for (int i = 0; i < key.length; i++) + { + if (key[i] != okey[i]) + return false; + } + return algorithm.equals(((SecretKeySpec) o).getAlgorithm()); + } + else + { + return false; + } + } + + public int hashCode() + { + int code = 0; + for (int i = 0; i < key.length; i++) + { + code ^= (key[i] & 0xff) << (i << 3 & 31); + } + return code ^ algorithm.hashCode(); + } +} diff --git a/libjava/classpath/javax/crypto/spec/package.html b/libjava/classpath/javax/crypto/spec/package.html new file mode 100644 index 000000000..8580c2f42 --- /dev/null +++ b/libjava/classpath/javax/crypto/spec/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in javax.crypto.spec package. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - javax.crypto.spec</title></head> + +<body> +<p></p> + +</body> +</html> |