1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.crypto.spec; 27 28 import java.security.spec.AlgorithmParameterSpec; 29 import java.util.Objects; 30 31 /** 32 * This class specifies the parameters used with the 33 * <a href="https://tools.ietf.org/html/rfc7539"><i>ChaCha20</i></a> 34 * algorithm. 35 * 36 * <p> The parameters consist of a 12-byte nonce and an initial 37 * counter value expressed as a 32-bit integer. 38 * 39 * <p> This class can be used to initialize a {@code Cipher} object that 40 * implements the <i>ChaCha20</i> algorithm. 41 * 42 * @since 11 43 */ 44 public final class ChaCha20ParameterSpec implements AlgorithmParameterSpec { 45 46 // The nonce length is defined by the spec as 96 bits (12 bytes) in length. 47 private static final int NONCE_LENGTH = 12; 48 49 private final byte[] nonce; 50 private final int counter; 51 52 /** 53 * Constructs a parameter set for ChaCha20 from the given nonce 54 * and counter. 55 * 56 * @param nonce a 12-byte nonce value 57 * @param counter the initial counter value 58 * 59 * @throws NullPointerException if {@code nonce} is {@code null} 60 * @throws IllegalArgumentException if {@code nonce} is not 12 bytes 61 * in length 62 */ ChaCha20ParameterSpec(byte[] nonce, int counter)63 public ChaCha20ParameterSpec(byte[] nonce, int counter) { 64 this.counter = counter; 65 66 Objects.requireNonNull(nonce, "Nonce must be non-null"); 67 this.nonce = nonce.clone(); 68 if (this.nonce.length != NONCE_LENGTH) { 69 throw new IllegalArgumentException( 70 "Nonce must be 12-bytes in length"); 71 } 72 } 73 74 /** 75 * Returns the nonce value. 76 * 77 * @return the nonce value. This method returns a new array each time 78 * this method is called. 79 */ getNonce()80 public byte[] getNonce() { 81 return nonce.clone(); 82 } 83 84 /** 85 * Returns the configured counter value. 86 * 87 * @return the counter value 88 */ getCounter()89 public int getCounter() { 90 return counter; 91 } 92 } 93