< prev index next > test/jdk/sun/security/ec/ed/TestEdOps.java
Print this page
/*
* @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
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 {
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 {
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");
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");
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);
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);
"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
"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
}
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);
}
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 >