Path: blob/master/src/java.base/share/classes/javax/crypto/spec/RC5ParameterSpec.java
41159 views
/*1* Copyright (c) 1998, 2020, 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) throw new IllegalArgumentException("IV missing");117int blockSize = (wordSize / 8) * 2;118if (iv.length - offset < blockSize) {119throw new IllegalArgumentException("IV too short");120}121this.iv = new byte[blockSize];122System.arraycopy(iv, offset, this.iv, 0, blockSize);123}124125/**126* Returns the version.127*128* @return the version.129*/130public int getVersion() {131return this.version;132}133134/**135* Returns the number of rounds.136*137* @return the number of rounds.138*/139public int getRounds() {140return this.rounds;141}142143/**144* Returns the word size in bits.145*146* @return the word size in bits.147*/148public int getWordSize() {149return this.wordSize;150}151152/**153* Returns the IV or null if this parameter set does not contain an IV.154*155* @return the IV or null if this parameter set does not contain an IV.156* Returns a new array each time this method is called.157*/158public byte[] getIV() {159return (iv == null? null:iv.clone());160}161162/**163* Tests for equality between the specified object and this164* object. Two RC5ParameterSpec objects are considered equal if their165* version numbers, number of rounds, word sizes, and IVs are equal.166* (Two IV references are considered equal if both are {@code null}.)167*168* @param obj the object to test for equality with this object.169*170* @return true if the objects are considered equal, false if171* {@code obj} is null or otherwise.172*/173public boolean equals(Object obj) {174if (obj == this) {175return true;176}177if (!(obj instanceof RC5ParameterSpec)) {178return false;179}180RC5ParameterSpec other = (RC5ParameterSpec) obj;181182return ((version == other.version) &&183(rounds == other.rounds) &&184(wordSize == other.wordSize) &&185java.util.Arrays.equals(iv, other.iv));186}187188/**189* Calculates a hash code value for the object.190* Objects that are equal will also have the same hashcode.191*/192public int hashCode() {193int retval = 0;194if (iv != null) {195for (int i = 1; i < iv.length; i++) {196retval += iv[i] * i;197}198}199retval += (version + rounds + wordSize);200return retval;201}202}203204205