Path: blob/master/src/java.base/share/classes/javax/crypto/spec/GCMParameterSpec.java
41159 views
/*1* Copyright (c) 2011, 2018, 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* Specifies the set of parameters required by a {@link31* javax.crypto.Cipher} using the Galois/Counter Mode (GCM) mode.32* <p>33* Simple block cipher modes (such as CBC) generally require only an34* initialization vector (such as {@code IvParameterSpec}),35* but GCM needs these parameters:36* <ul>37* <li>{@code IV}: Initialization Vector (IV) </li>38* <li>{@code tLen}: length (in bits) of authentication tag T</li>39* </ul>40* <p>41* In addition to the parameters described here, other GCM inputs/output42* (Additional Authenticated Data (AAD), Keys, block ciphers,43* plain/ciphertext and authentication tags) are handled in the {@code44* Cipher} class.45* <p>46* Please see <a href="http://www.ietf.org/rfc/rfc5116.txt"> RFC 511647* </a> for more information on the Authenticated Encryption with48* Associated Data (AEAD) algorithm, and <a href=49* "http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf">50* NIST Special Publication 800-38D</a>, "NIST Recommendation for Block51* Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC."52* <p>53* The GCM specification states that {@code tLen} may only have the54* values {128, 120, 112, 104, 96}, or {64, 32} for certain55* applications. Other values can be specified for this class, but not56* all CSP implementations will support them.57*58* @see javax.crypto.Cipher59*60* @since 1.761*/62public class GCMParameterSpec implements AlgorithmParameterSpec {6364// Initialization Vector. Could use IvParameterSpec, but that65// would add extra copies.66private byte[] iv;6768// Required Tag length (in bits).69private int tLen;7071/**72* Constructs a GCMParameterSpec using the specified authentication73* tag bit-length and IV buffer.74*75* @param tLen the authentication tag length (in bits)76* @param src the IV source buffer. The contents of the buffer are77* copied to protect against subsequent modification.78*79* @throws IllegalArgumentException if {@code tLen} is negative,80* or {@code src} is null.81*/82public GCMParameterSpec(int tLen, byte[] src) {83if (src == null) {84throw new IllegalArgumentException("src array is null");85}8687init(tLen, src, 0, src.length);88}8990/**91* Constructs a GCMParameterSpec object using the specified92* authentication tag bit-length and a subset of the specified93* buffer as the IV.94*95* @param tLen the authentication tag length (in bits)96* @param src the IV source buffer. The contents of the97* buffer are copied to protect against subsequent modification.98* @param offset the offset in {@code src} where the IV starts99* @param len the number of IV bytes100*101* @throws IllegalArgumentException if {@code tLen} is negative,102* {@code src} is null, {@code len} or {@code offset} is negative,103* or the sum of {@code offset} and {@code len} is greater than the104* length of the {@code src} byte array.105*/106public GCMParameterSpec(int tLen, byte[] src, int offset, int len) {107init(tLen, src, offset, len);108}109110/*111* Check input parameters.112*/113private void init(int tLen, byte[] src, int offset, int len) {114if (tLen < 0) {115throw new IllegalArgumentException(116"Length argument is negative");117}118this.tLen = tLen;119120// Input sanity check121if ((src == null) ||(len < 0) || (offset < 0)122|| (len > (src.length - offset))) {123throw new IllegalArgumentException("Invalid buffer arguments");124}125126iv = new byte[len];127System.arraycopy(src, offset, iv, 0, len);128}129130/**131* Returns the authentication tag length.132*133* @return the authentication tag length (in bits)134*/135public int getTLen() {136return tLen;137}138139/**140* Returns the Initialization Vector (IV).141*142* @return the IV. Creates a new array each time this method143* is called.144*/145public byte[] getIV() {146return iv.clone();147}148}149150151