Path: blob/master/src/java.base/share/classes/javax/crypto/spec/ChaCha20ParameterSpec.java
41159 views
/*1* Copyright (c) 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;28import java.util.Objects;2930/**31* This class specifies the parameters used with the32* <a href="https://tools.ietf.org/html/rfc7539"><i>ChaCha20</i></a>33* algorithm.34*35* <p> The parameters consist of a 12-byte nonce and an initial36* counter value expressed as a 32-bit integer.37*38* <p> This class can be used to initialize a {@code Cipher} object that39* implements the <i>ChaCha20</i> algorithm.40*41* @since 1142*/43public final class ChaCha20ParameterSpec implements AlgorithmParameterSpec {4445// The nonce length is defined by the spec as 96 bits (12 bytes) in length.46private static final int NONCE_LENGTH = 12;4748private final byte[] nonce;49private final int counter;5051/**52* Constructs a parameter set for ChaCha20 from the given nonce53* and counter.54*55* @param nonce a 12-byte nonce value56* @param counter the initial counter value57*58* @throws NullPointerException if {@code nonce} is {@code null}59* @throws IllegalArgumentException if {@code nonce} is not 12 bytes60* in length61*/62public ChaCha20ParameterSpec(byte[] nonce, int counter) {63this.counter = counter;6465Objects.requireNonNull(nonce, "Nonce must be non-null");66this.nonce = nonce.clone();67if (this.nonce.length != NONCE_LENGTH) {68throw new IllegalArgumentException(69"Nonce must be 12-bytes in length");70}71}7273/**74* Returns the nonce value.75*76* @return the nonce value. This method returns a new array each time77* this method is called.78*/79public byte[] getNonce() {80return nonce.clone();81}8283/**84* Returns the configured counter value.85*86* @return the counter value87*/88public int getCounter() {89return counter;90}91}929394