jdk/src/java.base/share/classes/javax/crypto/spec/RC5ParameterSpec.java
author avstepan
Tue, 25 Aug 2015 18:45:09 +0300
changeset 32275 17eeb583a331
parent 28301 6a8523efd309
permissions -rw-r--r--
8133802: replace some <tt> tags (obsolete in html5) in security-libs docs Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28301
6a8523efd309 8058912: Broken link (access denied error) to http://www.rsasecurity.com in RC5ParameterSpec
jnimeh
parents: 25859
diff changeset
     2
 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.crypto.spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class specifies the parameters used with the
28301
6a8523efd309 8058912: Broken link (access denied error) to http://www.rsasecurity.com in RC5ParameterSpec
jnimeh
parents: 25859
diff changeset
    32
 * <a href="http://tools.ietf.org/html/rfc2040"><i>RC5</i></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> The parameters consist of a version number, a rounds count, a word
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * size, and optionally an initialization vector (IV) (only in feedback mode).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
    38
 * <p> This class can be used to initialize a {@code Cipher} object that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * implements the <i>RC5</i> algorithm as supplied by
28301
6a8523efd309 8058912: Broken link (access denied error) to http://www.rsasecurity.com in RC5ParameterSpec
jnimeh
parents: 25859
diff changeset
    40
 * <a href="http://www.rsa.com">RSA Security LLC</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * or any parties authorized by RSA Security.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class RC5ParameterSpec implements AlgorithmParameterSpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private byte[] iv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private int version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private int rounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int wordSize; // the word size in bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Constructs a parameter set for RC5 from the given version, number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * rounds and word size (in bits).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param version the version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param rounds the number of rounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param wordSize the word size in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public RC5ParameterSpec(int version, int rounds, int wordSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.version = version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.rounds = rounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.wordSize = wordSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Constructs a parameter set for RC5 from the given version, number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * rounds, word size (in bits), and IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <p> Note that the size of the IV (block size) must be twice the word
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * size. The bytes that constitute the IV are those between
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
    74
     * {@code iv[0]} and {@code iv[2*(wordSize/8)-1]} inclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param version the version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param rounds the number of rounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param wordSize the word size in bits.
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
    79
     * @param iv the buffer with the IV. The first {@code 2*(wordSize/8)}
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
    80
     * bytes of the buffer are copied to protect against subsequent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * modification.
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
    82
     * @exception IllegalArgumentException if {@code iv} is
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
    83
     * {@code null} or {@code (iv.length < 2 * (wordSize / 8))}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public RC5ParameterSpec(int version, int rounds, int wordSize, byte[] iv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this(version, rounds, wordSize, iv, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Constructs a parameter set for RC5 from the given version, number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * rounds, word size (in bits), and IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
    93
     * <p> The IV is taken from {@code iv}, starting at
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
    94
     * {@code offset} inclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Note that the size of the IV (block size), starting at
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
    96
     * {@code offset} inclusive, must be twice the word size.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The bytes that constitute the IV are those between
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
    98
     * {@code iv[offset]} and {@code iv[offset+2*(wordSize/8)-1]}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param version the version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param rounds the number of rounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param wordSize the word size in bits.
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
   104
     * @param iv the buffer with the IV. The first {@code 2*(wordSize/8)}
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
   105
     * bytes of the buffer beginning at {@code offset}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * inclusive are copied to protect against subsequent modification.
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
   107
     * @param offset the offset in {@code iv} where the IV starts.
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
   108
     * @exception IllegalArgumentException if {@code iv} is
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
   109
     * {@code null} or
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 10336
diff changeset
   110
     * {@code (iv.length - offset < 2 * (wordSize / 8))}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public RC5ParameterSpec(int version, int rounds, int wordSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            byte[] iv, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.version = version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.rounds = rounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.wordSize = wordSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (iv == null) throw new IllegalArgumentException("IV missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int blockSize = (wordSize / 8) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (iv.length - offset < blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalArgumentException("IV too short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.iv = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.arraycopy(iv, offset, this.iv, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Returns the version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @return the version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public int getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return this.version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Returns the number of rounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return the number of rounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public int getRounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return this.rounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns the word size in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the word size in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public int getWordSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return this.wordSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns the IV or null if this parameter set does not contain an IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return the IV or null if this parameter set does not contain an IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Returns a new array each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public byte[] getIV() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   160
        return (iv == null? null:iv.clone());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Tests for equality between the specified object and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * object. Two RC5ParameterSpec objects are considered equal if their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * version numbers, number of rounds, word sizes, and IVs are equal.
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
   167
     * (Two IV references are considered equal if both are {@code null}.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return true if the objects are considered equal, false if
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28301
diff changeset
   172
     * {@code obj} is null or otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (!(obj instanceof RC5ParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        RC5ParameterSpec other = (RC5ParameterSpec) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return ((version == other.version) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                (rounds == other.rounds) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                (wordSize == other.wordSize) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                java.util.Arrays.equals(iv, other.iv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Calculates a hash code value for the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Objects that are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (iv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            for (int i = 1; i < iv.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                retval += iv[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        retval += (version + rounds + wordSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}