< prev index next >

test/jdk/com/sun/crypto/provider/Cipher/ChaCha20/ChaCha20Poly1305ParamTest.java

Print this page

  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  * @build jdk.test.lib.Convert
 29  * @run main ChaCha20Poly1305ParamTest
 30  * @summary ChaCha20 Cipher Implementation (parameters)
 31  */
 32 
 33 import java.util.*;
 34 import java.io.IOException;
 35 import java.security.GeneralSecurityException;
 36 import javax.crypto.Cipher;
 37 import javax.crypto.SecretKey;
 38 import javax.crypto.spec.ChaCha20ParameterSpec;
 39 import javax.crypto.spec.SecretKeySpec;
 40 import javax.crypto.AEADBadTagException;
 41 import java.security.spec.AlgorithmParameterSpec;
 42 import java.security.AlgorithmParameters;
 43 import java.security.NoSuchAlgorithmException;
 44 import java.nio.ByteBuffer;
 45 import jdk.test.lib.Convert;
 46 
 47 public class ChaCha20Poly1305ParamTest {
 48     public static class TestData {
 49         public TestData(String name, String keyStr, String nonceStr, int ctr,
 50                 int dir, String inputStr, String aadStr, String outStr) {
 51             testName = Objects.requireNonNull(name);
 52             key = Convert.hexStringToByteArray(Objects.requireNonNull(keyStr));
 53             nonce = Convert.hexStringToByteArray(
 54                     Objects.requireNonNull(nonceStr));
 55             if ((counter = ctr) < 0) {
 56                 throw new IllegalArgumentException(
 57                         "counter must be 0 or greater");
 58             }
 59             direction = dir;
 60             if ((direction != Cipher.ENCRYPT_MODE) &&
 61                     (direction != Cipher.DECRYPT_MODE)) {
 62                 throw new IllegalArgumentException(
 63                         "Direction must be ENCRYPT_MODE or DECRYPT_MODE");
 64             }
 65             input = Convert.hexStringToByteArray(
 66                     Objects.requireNonNull(inputStr));
 67             aad = (aadStr != null) ?
 68                 Convert.hexStringToByteArray(aadStr) : null;
 69             expOutput = Convert.hexStringToByteArray(
 70                     Objects.requireNonNull(outStr));
 71         }
 72 
 73         public final String testName;
 74         public final byte[] key;
 75         public final byte[] nonce;
 76         public final int counter;
 77         public final int direction;
 78         public final byte[] input;
 79         public final byte[] aad;
 80         public final byte[] expOutput;
 81     }
 82 
 83     public static final List<TestData> aeadTestList =
 84             new LinkedList<TestData>() {{
 85         add(new TestData("RFC 7539 Sample AEAD Test Vector",
 86             "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f",
 87             "070000004041424344454647",
 88             1, Cipher.ENCRYPT_MODE,
 89             "4c616469657320616e642047656e746c656d656e206f662074686520636c6173" +
 90             "73206f66202739393a204966204920636f756c64206f6666657220796f75206f" +

  1 /*
  2  * Copyright (c) 2018, 2020, 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 ChaCha20Poly1305ParamTest
 29  * @summary ChaCha20 Cipher Implementation (parameters)
 30  */
 31 
 32 import java.util.*;
 33 import java.io.IOException;
 34 import java.security.GeneralSecurityException;
 35 import javax.crypto.Cipher;
 36 import javax.crypto.SecretKey;
 37 import javax.crypto.spec.ChaCha20ParameterSpec;
 38 import javax.crypto.spec.SecretKeySpec;
 39 import javax.crypto.AEADBadTagException;
 40 import java.security.spec.AlgorithmParameterSpec;
 41 import java.security.AlgorithmParameters;
 42 import java.security.NoSuchAlgorithmException;
 43 import java.nio.ByteBuffer;

 44 
 45 public class ChaCha20Poly1305ParamTest {
 46     public static class TestData {
 47         public TestData(String name, String keyStr, String nonceStr, int ctr,
 48                 int dir, String inputStr, String aadStr, String outStr) {
 49             testName = Objects.requireNonNull(name);
 50             Hex.Decoder decoder = Hex.decoder();
 51             key = decoder.decode(keyStr);
 52             nonce = decoder.decode(nonceStr);
 53             if ((counter = ctr) < 0) {
 54                 throw new IllegalArgumentException(
 55                         "counter must be 0 or greater");
 56             }
 57             direction = dir;
 58             if ((direction != Cipher.ENCRYPT_MODE) &&
 59                     (direction != Cipher.DECRYPT_MODE)) {
 60                 throw new IllegalArgumentException(
 61                         "Direction must be ENCRYPT_MODE or DECRYPT_MODE");
 62             }
 63             input = decoder.decode(inputStr);
 64             aad = (aadStr != null) ? decoder.decode(aadStr) : null;
 65             expOutput = decoder.decode(outStr);



 66         }
 67 
 68         public final String testName;
 69         public final byte[] key;
 70         public final byte[] nonce;
 71         public final int counter;
 72         public final int direction;
 73         public final byte[] input;
 74         public final byte[] aad;
 75         public final byte[] expOutput;
 76     }
 77 
 78     public static final List<TestData> aeadTestList =
 79             new LinkedList<TestData>() {{
 80         add(new TestData("RFC 7539 Sample AEAD Test Vector",
 81             "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f",
 82             "070000004041424344454647",
 83             1, Cipher.ENCRYPT_MODE,
 84             "4c616469657320616e642047656e746c656d656e206f662074686520636c6173" +
 85             "73206f66202739393a204966204920636f756c64206f6666657220796f75206f" +
< prev index next >