RC5ParameterSpec.java revision 0
0N/A/*
2362N/A * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2362N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2362N/A * have any questions.
0N/A */
0N/A
0N/Apackage javax.crypto.spec;
0N/A
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/A
0N/A/**
0N/A * This class specifies the parameters used with the
0N/A * <a href="http://www.ietf.org/rfc/rfc2040.txt"><i>RC5</i></a>
0N/A * algorithm.
0N/A *
0N/A * <p> The parameters consist of a version number, a rounds count, a word
0N/A * size, and optionally an initialization vector (IV) (only in feedback mode).
0N/A *
0N/A * <p> This class can be used to initialize a <code>Cipher</code> object that
0N/A * implements the <i>RC5</i> algorithm as supplied by
0N/A * <a href="http://www.rsasecurity.com">RSA Security Inc.</a>,
0N/A * or any parties authorized by RSA Security.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A * @since 1.4
0N/A */
0N/Apublic class RC5ParameterSpec implements AlgorithmParameterSpec {
0N/A
0N/A private byte[] iv = null;
0N/A private int version;
0N/A private int rounds;
0N/A private int wordSize; // the word size in bits
0N/A
0N/A /**
0N/A * Constructs a parameter set for RC5 from the given version, number of
* rounds and word size (in bits).
*
* @param version the version.
* @param rounds the number of rounds.
* @param wordSize the word size in bits.
*/
public RC5ParameterSpec(int version, int rounds, int wordSize) {
this.version = version;
this.rounds = rounds;
this.wordSize = wordSize;
}
/**
* Constructs a parameter set for RC5 from the given version, number of
* rounds, word size (in bits), and IV.
*
* <p> Note that the size of the IV (block size) must be twice the word
* size. The bytes that constitute the IV are those between
* <code>iv[0]</code> and <code>iv[2*(wordSize/8)-1]</code> inclusive.
*
* @param version the version.
* @param rounds the number of rounds.
* @param wordSize the word size in bits.
* @param iv the buffer with the IV. The first <code>2*(wordSize/8)
* </code> bytes of the buffer are copied to protect against subsequent
* modification.
* @exception IllegalArgumentException if <code>iv</code> is
* <code>null</code> or <code>(iv.length < 2 * (wordSize / 8))</code>
*/
public RC5ParameterSpec(int version, int rounds, int wordSize, byte[] iv) {
this(version, rounds, wordSize, iv, 0);
}
/**
* Constructs a parameter set for RC5 from the given version, number of
* rounds, word size (in bits), and IV.
*
* <p> The IV is taken from <code>iv</code>, starting at
* <code>offset</code> inclusive.
* Note that the size of the IV (block size), starting at
* <code>offset</code> inclusive, must be twice the word size.
* The bytes that constitute the IV are those between
* <code>iv[offset]</code> and <code>iv[offset+2*(wordSize/8)-1]</code>
* inclusive.
*
* @param version the version.
* @param rounds the number of rounds.
* @param wordSize the word size in bits.
* @param iv the buffer with the IV. The first <code>2*(wordSize/8)
* </code> bytes of the buffer beginning at <code>offset</code>
* inclusive are copied to protect against subsequent modification.
* @param offset the offset in <code>iv</code> where the IV starts.
* @exception IllegalArgumentException if <code>iv</code> is
* <code>null</code> or
* <code>(iv.length - offset < 2 * (wordSize / 8))</code>
*/
public RC5ParameterSpec(int version, int rounds, int wordSize,
byte[] iv, int offset) {
this.version = version;
this.rounds = rounds;
this.wordSize = wordSize;
if (iv == null) throw new IllegalArgumentException("IV missing");
int blockSize = (wordSize / 8) * 2;
if (iv.length - offset < blockSize) {
throw new IllegalArgumentException("IV too short");
}
this.iv = new byte[blockSize];
System.arraycopy(iv, offset, this.iv, 0, blockSize);
}
/**
* Returns the version.
*
* @return the version.
*/
public int getVersion() {
return this.version;
}
/**
* Returns the number of rounds.
*
* @return the number of rounds.
*/
public int getRounds() {
return this.rounds;
}
/**
* Returns the word size in bits.
*
* @return the word size in bits.
*/
public int getWordSize() {
return this.wordSize;
}
/**
* Returns the IV or null if this parameter set does not contain an IV.
*
* @return the IV or null if this parameter set does not contain an IV.
* Returns a new array each time this method is called.
*/
public byte[] getIV() {
return (iv == null? null:(byte[])iv.clone());
}
/**
* Tests for equality between the specified object and this
* object. Two RC5ParameterSpec objects are considered equal if their
* version numbers, number of rounds, word sizes, and IVs are equal.
* (Two IV references are considered equal if both are <tt>null</tt>.)
*
* @param obj the object to test for equality with this object.
*
* @return true if the objects are considered equal, false if
* <code>obj</code> is null or otherwise.
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof RC5ParameterSpec)) {
return false;
}
RC5ParameterSpec other = (RC5ParameterSpec) obj;
return ((version == other.version) &&
(rounds == other.rounds) &&
(wordSize == other.wordSize) &&
java.util.Arrays.equals(iv, other.iv));
}
/**
* Calculates a hash code value for the object.
* Objects that are equal will also have the same hashcode.
*/
public int hashCode() {
int retval = 0;
if (iv != null) {
for (int i = 1; i < iv.length; i++) {
retval += iv[i] * i;
}
}
retval += (version + rounds + wordSize);
return retval;
}
}