< prev index next >

test/jdk/sun/security/ec/ed/TestEdOps.java

Print this page
*** 24,11 ***
  /*
   * @test
   * @bug 8166597
   * @summary Test EdDSA basic operations
   * @library /test/lib
-  * @build jdk.test.lib.Convert
   * @modules java.base/sun.security.provider
   *          java.base/sun.security.util
   *          java.base/sun.security.util.math
   *          java.base/sun.security.util.math.intpoly
   *          jdk.crypto.ec/sun.security.ec.ed
--- 24,10 ---

*** 37,12 ***
  
  import sun.security.ec.ed.*;
  import java.security.*;
  import java.security.spec.*;
  import java.util.Arrays;
  import sun.security.provider.SHAKE256;
- import jdk.test.lib.Convert;
  
  public class TestEdOps {
  
      public static void main(String[] args) throws Exception {
  
--- 36,13 ---
  
  import sun.security.ec.ed.*;
  import java.security.*;
  import java.security.spec.*;
  import java.util.Arrays;
+ import java.util.Hex;
+ 
  import sun.security.provider.SHAKE256;
  
  public class TestEdOps {
  
      public static void main(String[] args) throws Exception {
  

*** 69,12 ***
  
          System.out.println("SHAKE256 tests passed");
      }
  
      private static void runShakeTest(int outputLen, String msg, String digest) {
!         byte[] msgBytes = Convert.hexStringToByteArray(msg);
!         byte[] digestBytes = Convert.hexStringToByteArray(digest);
          SHAKE256 md = new SHAKE256(outputLen);
          md.update(msgBytes, 0, msgBytes.length);
          byte[] computed = md.digest();
          if (!Arrays.equals(digestBytes, computed)) {
              throw new RuntimeException("hash is incorrect");
--- 69,12 ---
  
          System.out.println("SHAKE256 tests passed");
      }
  
      private static void runShakeTest(int outputLen, String msg, String digest) {
!         byte[] msgBytes = Hex.decoder().decode(msg);
!         byte[] digestBytes = Hex.decoder().decode(digest);
          SHAKE256 md = new SHAKE256(outputLen);
          md.update(msgBytes, 0, msgBytes.length);
          byte[] computed = md.digest();
          if (!Arrays.equals(digestBytes, computed)) {
              throw new RuntimeException("hash is incorrect");

*** 176,18 ***
                                      String publicKey, String message,
                                      String signature) throws Exception {
  
          EdDSAParameterSpec sigParams = new EdDSAParameterSpec(false);
  
!         byte[] privKeyBytes = Convert.hexStringToByteArray(privateKey);
!         byte[] pubKeyBytes = Convert.hexStringToByteArray(publicKey);
!         byte[] msgBytes = Convert.hexStringToByteArray(message);
          byte[] computedSig = ops.sign(sigParams, privKeyBytes, msgBytes);
!         if (!Arrays.equals(computedSig,
-                            Convert.hexStringToByteArray(signature))) {
              throw new RuntimeException("Incorrect signature: " +
!                 Convert.byteArrayToHexString(computedSig) + " != " + signature);
          }
          // Test public key computation
          EdECPoint pubPoint = ops.computePublic(privKeyBytes);
          EdDSAPublicKeyImpl pubKey =
              new EdDSAPublicKeyImpl(ops.getParameters(), pubPoint);
--- 176,18 ---
                                      String publicKey, String message,
                                      String signature) throws Exception {
  
          EdDSAParameterSpec sigParams = new EdDSAParameterSpec(false);
  
!         Hex.Decoder decoder = Hex.decoder();
!         byte[] privKeyBytes = decoder.decode(privateKey);
!         byte[] pubKeyBytes = decoder.decode(publicKey);
+         byte[] msgBytes = decoder.decode(message);
          byte[] computedSig = ops.sign(sigParams, privKeyBytes, msgBytes);
!         if (!Arrays.equals(computedSig, decoder.decode(signature))) {
              throw new RuntimeException("Incorrect signature: " +
!                 Hex.encoder().encode(computedSig) + " != " + signature);
          }
          // Test public key computation
          EdECPoint pubPoint = ops.computePublic(privKeyBytes);
          EdDSAPublicKeyImpl pubKey =
              new EdDSAPublicKeyImpl(ops.getParameters(), pubPoint);

*** 260,11 ***
              "ab43ba28f430cdfe456ae531545f7ecd0ac834a55c9358c0372bfa0c6c6798c0" +
              "866aea01eb00742802b8438ea4cb82169c235160627b4c3a9480");
          // y value too large
          testInvalidPoint(NamedParameterSpec.ED448,
              "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
!             "fffffffffffffffffffffffffffffffffffffffffffffffffff00");
          // not square
          testInvalidPoint(NamedParameterSpec.ED448,
              "43ba28f430cdfe456ae531545f7ecd0ac834a55c9358c0372bfa0c6c6798c086" +
              "6aea01eb00742802b8438ea4cb82169c235160627b4c3a9480");
          // x = 0, but x mod 2 == 1
--- 260,11 ---
              "ab43ba28f430cdfe456ae531545f7ecd0ac834a55c9358c0372bfa0c6c6798c0" +
              "866aea01eb00742802b8438ea4cb82169c235160627b4c3a9480");
          // y value too large
          testInvalidPoint(NamedParameterSpec.ED448,
              "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
!             "ffffffffffffffffffffffffffffffffffffffffffffffffffff00");
          // not square
          testInvalidPoint(NamedParameterSpec.ED448,
              "43ba28f430cdfe456ae531545f7ecd0ac834a55c9358c0372bfa0c6c6798c086" +
              "6aea01eb00742802b8438ea4cb82169c235160627b4c3a9480");
          // x = 0, but x mod 2 == 1

*** 275,11 ***
      }
  
      private static void testInvalidPoint(NamedParameterSpec curve,
                                           String pointStr) throws Exception {
  
!         byte[] encodedPoint = Convert.hexStringToByteArray(pointStr);
          EdDSAParameters params =
              EdDSAParameters.get(RuntimeException::new, curve);
          EdDSAOperations ops = new EdDSAOperations(params);
          try {
              ops.decodeAffinePoint(InvalidKeyException::new, encodedPoint);
--- 275,11 ---
      }
  
      private static void testInvalidPoint(NamedParameterSpec curve,
                                           String pointStr) throws Exception {
  
!         byte[] encodedPoint = Hex.decoder().decode(pointStr);
          EdDSAParameters params =
              EdDSAParameters.get(RuntimeException::new, curve);
          EdDSAOperations ops = new EdDSAOperations(params);
          try {
              ops.decodeAffinePoint(InvalidKeyException::new, encodedPoint);
< prev index next >