< prev index next >

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

Print this page

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

 28  * @build jdk.test.lib.Convert
 29  * @run main ChaCha20KAT
 30  * @summary ChaCha20 Cipher Implementation (KAT)
 31  */
 32 
 33 import java.util.*;
 34 import java.security.GeneralSecurityException;
 35 import javax.crypto.Cipher;
 36 import javax.crypto.spec.ChaCha20ParameterSpec;
 37 import javax.crypto.spec.IvParameterSpec;
 38 import javax.crypto.spec.SecretKeySpec;
 39 import javax.crypto.AEADBadTagException;
 40 import java.nio.ByteBuffer;
 41 import jdk.test.lib.Convert;
 42 
 43 public class ChaCha20KAT {
 44     public static class TestData {
 45         public TestData(String name, String keyStr, String nonceStr, int ctr,
 46                 int dir, String inputStr, String aadStr, String outStr) {
 47             testName = Objects.requireNonNull(name);
 48             Hex.Decoder decoder = Hex.decoder();
 49             key = decoder.decode(Objects.requireNonNull(keyStr));
 50             nonce = decoder.decode(Objects.requireNonNull(nonceStr));
 51             if ((counter = ctr) < 0) {
 52                 throw new IllegalArgumentException(
 53                         "counter must be 0 or greater");
 54             }
 55             direction = dir;
 56             if ((direction != Cipher.ENCRYPT_MODE) &&
 57                     (direction != Cipher.DECRYPT_MODE)) {
 58                 throw new IllegalArgumentException(
 59                         "Direction must be ENCRYPT_MODE or DECRYPT_MODE");
 60             }
 61             input = decoder.decode(Objects.requireNonNull(inputStr));
 62             aad = (aadStr != null) ? decoder.decode(aadStr) : null;
 63             expOutput = decoder.decode(Objects.requireNonNull(outStr));



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