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 96-bit 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 public class ChaCha20ParameterSpec implements AlgorithmParameterSpec { 43 44 // The nonce length is defined by the spec as 96 bits in length. 45 private static final int NONCE_LENGTH = 12; 46 47 private final byte[] nonce; 48 private final int counter; 49 50 /** 51 * Constructs a parameter set for ChaCha20 from the given nonce 52 * and counter. 53 * 54 * @param nonce a 96-bit nonce value 55 * @param counter the initial counter value 56 * 57 * @throws NullPointerException if {@code nonce} is {@code null} 58 * @throws IllegalArgumentException if {@code nonce} is not 96-bits 59 * in length. 60 */ 61 public ChaCha20ParameterSpec(byte[] nonce, int counter) { 62 this.counter = counter; 63 64 Objects.requireNonNull(nonce, "Nonce must be non-null"); 65 if (nonce.length == 12) { 66 this.nonce = nonce.clone(); 67 } else { 68 throw new IllegalArgumentException( 69 "Nonce must be 96-bits in length"); 70 } 71 } 72 73 /** 74 * Returns the nonce value. 75 * 76 * @return the nonce value. This method returns a new array each time 77 * this method is called. 78 */ 79 public byte[] getNonce() { 80 return nonce.clone(); 81 } 82 83 /** 84 * Returns the configured counter value. 85 * 86 * @return the counter value. 87 */ 88 public int getCounter() { 89 return counter; 90 } 91 }