Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/GCMParameters.java
41161 views
/*1* Copyright (c) 2013, 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 com.sun.crypto.provider;2627import java.io.IOException;28import java.security.AlgorithmParametersSpi;29import java.security.spec.AlgorithmParameterSpec;30import java.security.spec.InvalidParameterSpecException;31import javax.crypto.spec.GCMParameterSpec;32import sun.security.util.HexDumpEncoder;33import sun.security.util.*;3435/**36* This class implements the parameter set used with37* GCM encryption, which is defined in RFC 5084 as follows:38*39* <pre>40* GCMParameters ::= SEQUENCE {41* aes-iv OCTET STRING, -- recommended size is 12 octets42* aes-tLen AES-GCM-ICVlen DEFAULT 12 }43*44* AES-GCM-ICVlen ::= INTEGER (12 | 13 | 14 | 15 | 16)45*46* </pre>47*48* @author Valerie Peng49* @since 1.850*/51public final class GCMParameters extends AlgorithmParametersSpi {5253// the iv54private byte[] iv;55// the tag length in bytes56private int tLen;5758public GCMParameters() {}5960protected void engineInit(AlgorithmParameterSpec paramSpec)61throws InvalidParameterSpecException {6263if (!(paramSpec instanceof GCMParameterSpec)) {64throw new InvalidParameterSpecException65("Inappropriate parameter specification");66}67GCMParameterSpec gps = (GCMParameterSpec) paramSpec;68// need to convert from bits to bytes for ASN.1 encoding69this.tLen = gps.getTLen()/8;70this.iv = gps.getIV();71}7273protected void engineInit(byte[] encoded) throws IOException {74DerValue val = new DerValue(encoded);75// check if IV or params76if (val.tag == DerValue.tag_Sequence) {77byte[] iv = val.data.getOctetString();78int tLen;79if (val.data.available() != 0) {80tLen = val.data.getInteger();81if (tLen < 12 || tLen > 16 ) {82throw new IOException83("GCM parameter parsing error: unsupported tag len: " +84tLen);85}86if (val.data.available() != 0) {87throw new IOException88("GCM parameter parsing error: extra data");89}90} else {91tLen = 12;92}93this.iv = iv.clone();94this.tLen = tLen;95} else {96throw new IOException("GCM parameter parsing error: no SEQ tag");97}98}99100protected void engineInit(byte[] encoded, String decodingMethod)101throws IOException {102engineInit(encoded);103}104105protected <T extends AlgorithmParameterSpec>106T engineGetParameterSpec(Class<T> paramSpec)107throws InvalidParameterSpecException {108109if (GCMParameterSpec.class.isAssignableFrom(paramSpec)) {110return paramSpec.cast(new GCMParameterSpec(tLen * 8, iv));111} else {112throw new InvalidParameterSpecException113("Inappropriate parameter specification");114}115}116117protected byte[] engineGetEncoded() throws IOException {118DerOutputStream out = new DerOutputStream();119DerOutputStream bytes = new DerOutputStream();120121bytes.putOctetString(iv);122bytes.putInteger(tLen);123out.write(DerValue.tag_Sequence, bytes);124return out.toByteArray();125}126127protected byte[] engineGetEncoded(String encodingMethod)128throws IOException {129return engineGetEncoded();130}131132/*133* Returns a formatted string describing the parameters.134*/135protected String engineToString() {136String LINE_SEP = System.lineSeparator();137HexDumpEncoder encoder = new HexDumpEncoder();138StringBuilder sb139= new StringBuilder(LINE_SEP + " iv:" + LINE_SEP + "["140+ encoder.encodeBuffer(iv) + "]");141142sb.append(LINE_SEP + "tLen(bits):" + LINE_SEP + tLen*8 + LINE_SEP);143return sb.toString();144}145}146147148