1 /* 2 * Copyright 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package javax.crypto.spec; 18 19 import java.security.spec.AlgorithmParameterSpec; 20 import java.util.Arrays; 21 22 /** 23 * Provides a the parameters for an instance of a {@link javax.crypto.Cipher} 24 * using Galois/Counter Mode (GCM). This is an Authenticated Encryption with 25 * Associated Data (AEAD) mode for a cipher which allows you to use the 26 * {@link javax.crypto.Cipher#updateAAD(byte[])} method to provide data that is 27 * transmitted in the clear but authenticated using a cryptographic Message 28 * Authentication Code (MAC). 29 * 30 * @since 1.7 31 */ 32 public class GCMParameterSpec implements AlgorithmParameterSpec { 33 private final int tagLen; 34 35 private final byte[] iv; 36 37 /** 38 * Creates a new {@code GCMParameterSpec} instance from the specified 39 * Initial Vector (IV) from buffer {@code iv} and a tag length of 40 * {@code tagLen} in bits. 41 * 42 * @throws IllegalArgumentException if the specified {@code iv} is null or 43 * {@code offset} and {@code byteCount} do not specify a valid 44 * chunk in the specified buffer. 45 */ 46 public GCMParameterSpec(int tagLen, byte[] iv) { 47 if (tagLen < 0) { 48 throw new IllegalArgumentException("tag should be a non-negative integer"); 49 } 50 if (iv == null) { 51 throw new IllegalArgumentException("iv == null"); 52 } 53 this.tagLen = tagLen; 54 this.iv = iv.clone(); 55 } 56 57 /** 58 * Creates a new {@code GCMParameterSpec} instance with the Initial Vector 59 * (IV) of {@code byteCount} bytes from the specified buffer {@code iv} 60 * starting at {@code offset} and a tag length of {@code tagLen} in bits. 61 * 62 * @throws IllegalArgumentException if the specified {@code iv} is null or 63 * {@code offset} and {@code byteCount} do not specify a valid 64 * chunk in the specified buffer. 65 * @throws ArrayIndexOutOfBoundsException if {@code offset} or 66 * {@code byteCount} are negative. 67 */ 68 public GCMParameterSpec(int tagLen, byte[] iv, int offset, int byteCount) { 69 if (tagLen < 0) { 70 throw new IllegalArgumentException("tag should be a non-negative integer"); 71 } 72 if (iv == null) { 73 throw new IllegalArgumentException("iv == null"); 74 } 75 try { 76 Arrays.checkOffsetAndCount(iv.length, offset, byteCount); 77 } catch (ArrayIndexOutOfBoundsException e) { 78 throw new IllegalArgumentException(e); 79 } 80 this.tagLen = tagLen; 81 this.iv = Arrays.copyOfRange(iv, offset, offset + byteCount); 82 } 83 84 /** 85 * Returns the size of the tag in bits. 86 */ 87 public int getTLen() { 88 return tagLen; 89 } 90 91 /** 92 * Returns the Initial Vector (IV) used by this parameter spec. 93 */ 94 public byte[] getIV() { 95 return iv.clone(); 96 } 97 } 98