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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8153029 27 * @library /test/lib 28 * @run main ChaCha20Poly1305ParametersUnitTest 29 * @summary Unit test for com.sun.crypto.provider.ChaCha20Poly1305Parameters. 30 */ 31 32 import java.io.IOException; 33 import java.security.AlgorithmParameters; 34 import java.security.spec.InvalidParameterSpecException; 35 import java.util.Arrays; 36 37 import javax.crypto.spec.ChaCha20ParameterSpec; 38 import javax.crypto.spec.IvParameterSpec; 39 40 public class ChaCha20Poly1305ParametersUnitTest { 41 42 private static final String ALGORITHM = "ChaCha20-Poly1305"; 43 44 private static final byte[] NONCE = { 45 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8 }; 46 private static final byte[] PARAM = { 47 4, 12, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8 }; 48 49 private static final byte[] BAD_NONCE = { 50 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 51 private static final byte[] BAD_PARAM = { 52 4, 13, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 53 main(String[] args)54 public static void main(String[] args) throws Exception { 55 testInit(); 56 testGetParameterSpec(); 57 testGetEncoded(); 58 } 59 testInit()60 private static void testInit() throws Exception { 61 System.out.println("== init =="); 62 63 AlgorithmParameters ap = AlgorithmParameters.getInstance(ALGORITHM); 64 ap.init(new IvParameterSpec(NONCE)); 65 System.out.println("AlgorithmParameters: " + ap); 66 67 ap = AlgorithmParameters.getInstance(ALGORITHM); 68 ap.init(PARAM); 69 70 ap = AlgorithmParameters.getInstance(ALGORITHM); 71 try { 72 ap.init(new ChaCha20ParameterSpec(NONCE, 0)); 73 throw new RuntimeException("IvParameterSpec is needed"); 74 } catch (InvalidParameterSpecException e) { 75 System.out.println("Expected " + e); 76 } 77 78 ap = AlgorithmParameters.getInstance(ALGORITHM); 79 try { 80 ap.init(new IvParameterSpec(BAD_NONCE)); 81 throw new RuntimeException("Nonce must be 96 bits in length"); 82 } catch (InvalidParameterSpecException e) { 83 System.out.println("Expected " + e); 84 } 85 86 ap = AlgorithmParameters.getInstance(ALGORITHM); 87 try { 88 ap.init(BAD_PARAM); 89 throw new RuntimeException("Nonce must be 96 bits in length"); 90 } catch (IOException e) { 91 System.out.println("Expected " + e); 92 } 93 } 94 testGetParameterSpec()95 private static void testGetParameterSpec() throws Exception { 96 System.out.println("== getParameterSpec =="); 97 98 AlgorithmParameters ap = AlgorithmParameters.getInstance(ALGORITHM); 99 ap.init(PARAM); 100 101 IvParameterSpec paramSpec = ap.getParameterSpec(IvParameterSpec.class); 102 byte[] nonce = paramSpec.getIV(); 103 System.out.println("Nonce: " + Arrays.toString(nonce)); 104 Arrays.equals(nonce, NONCE); 105 106 try { 107 ap.getParameterSpec(ChaCha20ParameterSpec.class); 108 throw new RuntimeException("IvParameterSpec is needed"); 109 } catch (InvalidParameterSpecException e) { 110 System.out.println("Expected " + e); 111 } 112 } 113 testGetEncoded()114 private static void testGetEncoded() throws Exception { 115 System.out.println("== getEncoded =="); 116 117 AlgorithmParameters ap = AlgorithmParameters.getInstance(ALGORITHM); 118 ap.init(PARAM); 119 120 byte[] defaultFormatEncoded = ap.getEncoded(); 121 System.out.println("Default format encoding: " 122 + Arrays.toString(defaultFormatEncoded)); 123 if (!Arrays.equals(defaultFormatEncoded, PARAM)) { 124 throw new RuntimeException("Default format encoding failed"); 125 } 126 127 byte[] asn1FormatEncoded = ap.getEncoded("ASN.1"); 128 System.out.println("ASN.1 format encoding: " 129 + Arrays.toString(asn1FormatEncoded)); 130 if (!Arrays.equals(asn1FormatEncoded, PARAM)) { 131 throw new RuntimeException("ASN.1 format encoding failed"); 132 } 133 134 byte[] nullFormatEncoded = ap.getEncoded(null); 135 System.out.println("Null format encoding: " 136 + Arrays.toString(nullFormatEncoded)); 137 if (!Arrays.equals(nullFormatEncoded, PARAM)) { 138 throw new RuntimeException("Null format encoding failed"); 139 } 140 141 try { 142 ap.getEncoded("BAD"); 143 throw new RuntimeException("Format must be ASN.1"); 144 } catch (IOException e) { 145 System.out.println("Expected " + e); 146 } 147 } 148 } 149