Path: blob/master/src/java.base/share/classes/javax/crypto/spec/RC5ParameterSpec.java
67760 views
/*1* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.crypto.spec;2627import java.security.spec.AlgorithmParameterSpec;2829/**30* This class specifies the parameters used with the31* <a href="http://tools.ietf.org/html/rfc2040"><i>RC5</i></a>32* algorithm.33*34* <p> The parameters consist of a version number, a rounds count, a word35* size, and optionally an initialization vector (IV) (only in feedback mode).36*37* <p> This class can be used to initialize a {@code Cipher} object that38* implements the <i>RC5</i> algorithm as supplied by39* <a href="http://www.rsa.com">RSA Security LLC</a>,40* or any parties authorized by RSA Security.41*42* @author Jan Luehe43*44* @since 1.445*/46public class RC5ParameterSpec implements AlgorithmParameterSpec {4748private byte[] iv = null;49private int version;50private int rounds;51private int wordSize; // the word size in bits5253/**54* Constructs a parameter set for RC5 from the given version, number of55* rounds and word size (in bits).56*57* @param version the version.58* @param rounds the number of rounds.59* @param wordSize the word size in bits.60*/61public RC5ParameterSpec(int version, int rounds, int wordSize) {62this.version = version;63this.rounds = rounds;64this.wordSize = wordSize;65}6667/**68* Constructs a parameter set for RC5 from the given version, number of69* rounds, word size (in bits), and IV.70*71* <p> Note that the size of the IV (block size) must be twice the word72* size. The bytes that constitute the IV are those between73* {@code iv[0]} and {@code iv[2*(wordSize/8)-1]} inclusive.74*75* @param version the version.76* @param rounds the number of rounds.77* @param wordSize the word size in bits.78* @param iv the buffer with the IV. The first {@code 2*(wordSize/8)}79* bytes of the buffer are copied to protect against subsequent80* modification.81* @exception IllegalArgumentException if {@code iv} is82* {@code null} or {@code (iv.length < 2 * (wordSize / 8))}83*/84public RC5ParameterSpec(int version, int rounds, int wordSize, byte[] iv) {85this(version, rounds, wordSize, iv, 0);86}8788/**89* Constructs a parameter set for RC5 from the given version, number of90* rounds, word size (in bits), and IV.91*92* <p> The IV is taken from {@code iv}, starting at93* {@code offset} inclusive.94* Note that the size of the IV (block size), starting at95* {@code offset} inclusive, must be twice the word size.96* The bytes that constitute the IV are those between97* {@code iv[offset]} and {@code iv[offset+2*(wordSize/8)-1]}98* inclusive.99*100* @param version the version.101* @param rounds the number of rounds.102* @param wordSize the word size in bits.103* @param iv the buffer with the IV. The first {@code 2*(wordSize/8)}104* bytes of the buffer beginning at {@code offset}105* inclusive are copied to protect against subsequent modification.106* @param offset the offset in {@code iv} where the IV starts.107* @exception IllegalArgumentException if {@code iv} is108* {@code null} or109* {@code (iv.length - offset < 2 * (wordSize / 8))}110*/111public RC5ParameterSpec(int version, int rounds, int wordSize,112byte[] iv, int offset) {113this.version = version;114this.rounds = rounds;115this.wordSize = wordSize;116if (iv == null) {117throw new IllegalArgumentException("IV missing");118}119if (offset < 0) {120throw new ArrayIndexOutOfBoundsException("offset is negative");121}122int blockSize = (wordSize / 8) * 2;123if (iv.length - offset < blockSize) {124throw new IllegalArgumentException("IV too short");125}126this.iv = new byte[blockSize];127System.arraycopy(iv, offset, this.iv, 0, blockSize);128}129130/**131* Returns the version.132*133* @return the version.134*/135public int getVersion() {136return this.version;137}138139/**140* Returns the number of rounds.141*142* @return the number of rounds.143*/144public int getRounds() {145return this.rounds;146}147148/**149* Returns the word size in bits.150*151* @return the word size in bits.152*/153public int getWordSize() {154return this.wordSize;155}156157/**158* Returns the IV or null if this parameter set does not contain an IV.159*160* @return the IV or null if this parameter set does not contain an IV.161* Returns a new array each time this method is called.162*/163public byte[] getIV() {164return (iv == null? null:iv.clone());165}166167/**168* Tests for equality between the specified object and this169* object. Two RC5ParameterSpec objects are considered equal if their170* version numbers, number of rounds, word sizes, and IVs are equal.171* (Two IV references are considered equal if both are {@code null}.)172*173* @param obj the object to test for equality with this object.174*175* @return true if the objects are considered equal, false if176* {@code obj} is null or otherwise.177*/178public boolean equals(Object obj) {179if (obj == this) {180return true;181}182if (!(obj instanceof RC5ParameterSpec)) {183return false;184}185RC5ParameterSpec other = (RC5ParameterSpec) obj;186187return ((version == other.version) &&188(rounds == other.rounds) &&189(wordSize == other.wordSize) &&190java.util.Arrays.equals(iv, other.iv));191}192193/**194* Calculates a hash code value for the object.195* Objects that are equal will also have the same hashcode.196*/197public int hashCode() {198int retval = 0;199if (iv != null) {200for (int i = 1; i < iv.length; i++) {201retval += iv[i] * i;202}203}204retval += (version + rounds + wordSize);205return retval;206}207}208209210