jdk/src/share/classes/javax/crypto/spec/IvParameterSpec.java
author alanb
Mon, 10 Jun 2013 12:58:32 +0100
changeset 18156 edb590d448c5
parent 10336 0bb1999251f8
child 23010 6dadb192ad81
permissions -rw-r--r--
8016217: More javadoc warnings Reviewed-by: lancea, chegar, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, 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 an <i>initialization vector</i> (IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Examples which use IVs are ciphers in feedback mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * e.g., DES in CBC mode and RSA ciphers with OAEP encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class IvParameterSpec implements AlgorithmParameterSpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private byte[] iv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Creates an IvParameterSpec object using the bytes in <code>iv</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * as the IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @param iv the buffer with the IV. The contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * buffer are copied to protect against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @throws NullPointerException if <code>iv</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public IvParameterSpec(byte[] iv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this(iv, 0, iv.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Creates an IvParameterSpec object using the first <code>len</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * bytes in <code>iv</code>, beginning at <code>offset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * inclusive, as the IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * <p> The bytes that constitute the IV are those between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <code>iv[offset]</code> and <code>iv[offset+len-1]</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param iv the buffer with the IV. The first <code>len</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * bytes of the buffer beginning at <code>offset</code> inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * are copied to protect against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param offset the offset in <code>iv</code> where the IV
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param len the number of IV bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @throws IllegalArgumentException if <code>iv</code> is <code>null</code>
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 10336
diff changeset
    71
     * or {@code (iv.length - offset < len)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @throws ArrayIndexOutOfBoundsException is thrown if <code>offset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * or <code>len</code> index bytes outside the <code>iv</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public IvParameterSpec(byte[] iv, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (iv == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new IllegalArgumentException("IV missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (iv.length - offset < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                ("IV buffer too short for given offset/length combination");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new ArrayIndexOutOfBoundsException("len is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.iv = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.arraycopy(iv, offset, this.iv, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Returns the initialization vector (IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @return the initialization vector (IV). Returns a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public byte[] getIV() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    97
        return this.iv.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
}