1 /* 2 * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.crypto.spec; 27 28 import java.security.spec.AlgorithmParameterSpec; 29 30 /** 31 * This class specifies an <i>initialization vector</i> (IV). 32 * Examples which use IVs are ciphers in feedback mode, 33 * e.g., DES in CBC mode and RSA ciphers with OAEP encoding 34 * operation. 35 * 36 * @author Jan Luehe 37 * 38 * @since 1.4 39 */ 40 public class IvParameterSpec implements AlgorithmParameterSpec { 41 42 private byte[] iv; 43 44 /** 45 * Creates an IvParameterSpec object using the bytes in <code>iv</code> 46 * as the IV. 47 * 48 * @param iv the buffer with the IV. The contents of the 49 * buffer are copied to protect against subsequent modification. 50 * @throws NullPointerException if <code>iv</code> is <code>null</code> 51 */ IvParameterSpec(byte[] iv)52 public IvParameterSpec(byte[] iv) { 53 this(iv, 0, iv.length); 54 } 55 56 /** 57 * Creates an IvParameterSpec object using the first <code>len</code> 58 * bytes in <code>iv</code>, beginning at <code>offset</code> 59 * inclusive, as the IV. 60 * 61 * <p> The bytes that constitute the IV are those between 62 * <code>iv[offset]</code> and <code>iv[offset+len-1]</code> inclusive. 63 * 64 * @param iv the buffer with the IV. The first <code>len</code> 65 * bytes of the buffer beginning at <code>offset</code> inclusive 66 * are copied to protect against subsequent modification. 67 * @param offset the offset in <code>iv</code> where the IV 68 * starts. 69 * @param len the number of IV bytes. 70 * @throws IllegalArgumentException if <code>iv</code> is <code>null</code> 71 * or {@code (iv.length - offset < len)} 72 * @throws ArrayIndexOutOfBoundsException is thrown if <code>offset</code> 73 * or <code>len</code> index bytes outside the <code>iv</code>. 74 */ IvParameterSpec(byte[] iv, int offset, int len)75 public IvParameterSpec(byte[] iv, int offset, int len) { 76 if (iv == null) { 77 throw new IllegalArgumentException("IV missing"); 78 } 79 if (offset < 0) { 80 throw new ArrayIndexOutOfBoundsException("offset is negative"); 81 } 82 if (len < 0) { 83 throw new ArrayIndexOutOfBoundsException("len is negative"); 84 } 85 if (iv.length - offset < len) { 86 throw new IllegalArgumentException 87 ("IV buffer too short for given offset/length combination"); 88 } 89 this.iv = new byte[len]; 90 System.arraycopy(iv, offset, this.iv, 0, len); 91 } 92 93 /** 94 * Returns the initialization vector (IV). 95 * 96 * @return the initialization vector (IV). Returns a new array 97 * each time this method is called. 98 */ getIV()99 public byte[] getIV() { 100 return this.iv.clone(); 101 } 102 } 103