Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/ChaCha20Poly1305Parameters.java
41161 views
/*1* Copyright (c) 2018, 2020, 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.IvParameterSpec;32import sun.security.util.*;3334/**35* This class implements the parameter set used with the ChaCha20-Poly130536* algorithm. The parameter definition comes from37* <a href="https://tools.ietf.org/html/rfc8103"><i>RFC 8103</i></a>38* and is defined according to the following ASN.1:39*40* <pre>41* id-alg-AEADChaCha20Poly1305 OBJECT IDENTIFIER ::=42* { iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1)43* pkcs9(9) smime(16) alg(3) 18 }44*45* AEADChaCha20Poly1305Nonce ::= OCTET STRING (SIZE(12))46* </pre>47*48* The AlgorithmParameters may be instantiated either by its name49* ("ChaCha20-Poly1305") or via its OID (1.2.840.113549.1.9.16.3.18)50*/51public final class ChaCha20Poly1305Parameters extends AlgorithmParametersSpi {5253private static final String DEFAULT_FMT = "ASN.1";54private byte[] nonce;5556public ChaCha20Poly1305Parameters() {}5758/**59* Initialize the ChaCha20Poly1305Parameters using an IvParameterSpec.60*61* @param paramSpec the {@code IvParameterSpec} used to configure62* this object.63*64* @throws InvalidParameterSpecException if an object of a type other65* than {@code IvParameterSpec} is used.66*/67@Override68protected void engineInit(AlgorithmParameterSpec paramSpec)69throws InvalidParameterSpecException {7071if (!(paramSpec instanceof IvParameterSpec)) {72throw new InvalidParameterSpecException73("Inappropriate parameter specification");74}75IvParameterSpec ivps = (IvParameterSpec)paramSpec;7677// Obtain the nonce78nonce = ivps.getIV();79if (nonce.length != 12) {80throw new InvalidParameterSpecException("ChaCha20-Poly1305 nonce" +81" must be 12 bytes in length");82}83}8485/**86* Initialize the ChaCha20Poly1305Parameters from a DER encoded87* parameter block.88*89* @param encoded the DER encoding of the nonce as an OCTET STRING.90*91* @throws IOException if the encoded nonce is not 12 bytes long or a DER92* decoding error occurs.93*/94@Override95protected void engineInit(byte[] encoded) throws IOException {96DerValue val = new DerValue(encoded);9798// Get the nonce value99nonce = val.getOctetString();100if (nonce.length != 12) {101throw new IOException(102"ChaCha20-Poly1305 nonce must be 12 bytes in length");103}104}105106/**107* Initialize the ChaCha20Poly1305Parameters from a DER encoded108* parameter block.109*110* @param encoded the DER encoding of the nonce and initial block counter.111* @param decodingMethod the decoding method. The only currently accepted112* value is "ASN.1"113*114* @throws IOException if the encoded nonce is not 12 bytes long, a DER115* decoding error occurs, or an unsupported decoding method is116* provided.117*/118@Override119protected void engineInit(byte[] encoded, String decodingMethod)120throws IOException {121if (decodingMethod == null ||122decodingMethod.equalsIgnoreCase(DEFAULT_FMT)) {123engineInit(encoded);124} else {125throw new IOException("Unsupported parameter format: " +126decodingMethod);127}128}129130/**131* Return an IvParameterSpec with the same parameters as those132* held in this object.133*134* @param paramSpec the class name of the spec. In this case it should135* be {@code IvParameterSpec.class}.136*137* @return a {@code IvParameterSpec} object containing the nonce138* value held in this object.139*140* @throws InvalidParameterSpecException if a class other than141* {@code IvParameterSpec.class} was specified in the paramSpec142* parameter.143*/144@Override145protected <T extends AlgorithmParameterSpec>146T engineGetParameterSpec(Class<T> paramSpec)147throws InvalidParameterSpecException {148149if (IvParameterSpec.class.isAssignableFrom(paramSpec)) {150return paramSpec.cast(new IvParameterSpec(nonce));151} else {152throw new InvalidParameterSpecException153("Inappropriate parameter specification");154}155}156157/**158* Return the encoded parameters in ASN.1 form.159*160* @return a byte array containing the DER-encoding for the161* ChaCha20-Poly1305 parameters. This will be the nonce162* encoded as a DER OCTET STRING.163*164* @throws IOException if any DER encoding error occurs.165*/166@Override167protected byte[] engineGetEncoded() throws IOException {168DerOutputStream out = new DerOutputStream();169out.write(DerValue.tag_OctetString, nonce);170return out.toByteArray();171}172173/**174* Return the encoded parameters in ASN.1 form.175*176* @param encodingMethod the encoding method to be used. This parameter177* must be "ASN.1" as it is the only currently supported encoding178* format. If the parameter is {@code null} then the default179* encoding format will be used.180*181* @return a byte array containing the DER-encoding for the182* ChaCha20-Poly1305 parameters.183*184* @throws IOException if any DER encoding error occurs or an unsupported185* encoding method is provided.186*/187@Override188protected byte[] engineGetEncoded(String encodingMethod)189throws IOException {190if (encodingMethod == null ||191encodingMethod.equalsIgnoreCase(DEFAULT_FMT)) {192return engineGetEncoded();193} else {194throw new IOException("Unsupported encoding format: " +195encodingMethod);196}197}198199/**200* Creates a formatted string describing the parameters.201*202* @return a string representation of the ChaCha20 parameters.203*/204@Override205protected String engineToString() {206String LINE_SEP = System.lineSeparator();207HexDumpEncoder encoder = new HexDumpEncoder();208StringBuilder sb = new StringBuilder(LINE_SEP + "nonce:" +209LINE_SEP + "[" + encoder.encodeBuffer(nonce) + "]");210return sb.toString();211}212}213214215