Search in sources :

Example 6 with EdDSAPublicKeySpec

use of net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec in project i2p.i2p by i2p.

the class EdDSAEngineTest method testVerify.

@Test
public void testVerify() throws Exception {
    // Signature sgr = Signature.getInstance("EdDSA", "I2P");
    Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
    for (Ed25519TestVectors.TestTuple testCase : Ed25519TestVectors.testCases) {
        EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(testCase.pk, spec);
        PublicKey vKey = new EdDSAPublicKey(pubKey);
        sgr.initVerify(vKey);
        sgr.update(testCase.message);
        assertThat("Test case " + testCase.caseNum + " failed", sgr.verify(testCase.sig), is(true));
    }
}
Also used : EdDSAParameterSpec(net.i2p.crypto.eddsa.spec.EdDSAParameterSpec) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) Test(org.junit.Test)

Example 7 with EdDSAPublicKeySpec

use of net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec in project i2p.i2p by i2p.

the class EdDSAEngineTest method testVerifyWrongSigLength.

/**
 * Checks that a wrong-length signature throws an IAE.
 */
@Test
public void testVerifyWrongSigLength() throws Exception {
    // Signature sgr = Signature.getInstance("EdDSA", "I2P");
    Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, EdDSANamedCurveTable.getByName("ed25519-sha-512"));
    PublicKey vKey = new EdDSAPublicKey(pubKey);
    sgr.initVerify(vKey);
    sgr.update(TEST_MSG);
    exception.expect(SignatureException.class);
    exception.expectMessage("signature length is wrong");
    sgr.verify(new byte[] { 0 });
}
Also used : EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) Test(org.junit.Test)

Example 8 with EdDSAPublicKeySpec

use of net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec in project i2p.i2p by i2p.

the class EdDSAEngineTest method testVerifyOneShotMode.

@Test
public void testVerifyOneShotMode() throws Exception {
    Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, EdDSANamedCurveTable.getByName("ed25519-sha-512"));
    PublicKey vKey = new EdDSAPublicKey(pubKey);
    sgr.initVerify(vKey);
    sgr.setParameter(EdDSAEngine.ONE_SHOT_MODE);
    sgr.update(TEST_MSG);
    assertThat("One-shot mode verify failed", sgr.verify(TEST_MSG_SIG), is(true));
}
Also used : EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) Test(org.junit.Test)

Example 9 with EdDSAPublicKeySpec

use of net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec in project i2p.i2p by i2p.

the class KeyGenerator method getSigningPublicKey.

/**
 * Convert a SigningPrivateKey to a SigningPublicKey.
 *  As of 0.9.16, supports all key types.
 *
 * @param priv a SigningPrivateKey object
 * @return a SigningPublicKey object
 * @throws IllegalArgumentException on bad key or unknown type
 */
public static SigningPublicKey getSigningPublicKey(SigningPrivateKey priv) {
    SigType type = priv.getType();
    if (type == null)
        throw new IllegalArgumentException("Unknown type");
    try {
        switch(type.getBaseAlgorithm()) {
            case DSA:
                BigInteger x = new NativeBigInteger(1, priv.toByteArray());
                BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
                SigningPublicKey pub = new SigningPublicKey();
                pub.setData(SigUtil.rectify(y, SigningPublicKey.KEYSIZE_BYTES));
                return pub;
            case EC:
                ECPrivateKey ecpriv = SigUtil.toJavaECKey(priv);
                BigInteger s = ecpriv.getS();
                ECParameterSpec spec = (ECParameterSpec) type.getParams();
                EllipticCurve curve = spec.getCurve();
                ECPoint g = spec.getGenerator();
                ECPoint w = ECUtil.scalarMult(g, s, curve);
                ECPublicKeySpec ecks = new ECPublicKeySpec(w, ecpriv.getParams());
                KeyFactory eckf = KeyFactory.getInstance("EC");
                ECPublicKey ecpub = (ECPublicKey) eckf.generatePublic(ecks);
                return SigUtil.fromJavaKey(ecpub, type);
            case RSA:
                RSAPrivateKey rsapriv = SigUtil.toJavaRSAKey(priv);
                BigInteger exp = ((RSAKeyGenParameterSpec) type.getParams()).getPublicExponent();
                RSAPublicKeySpec rsaks = new RSAPublicKeySpec(rsapriv.getModulus(), exp);
                KeyFactory rsakf = KeyFactory.getInstance("RSA");
                RSAPublicKey rsapub = (RSAPublicKey) rsakf.generatePublic(rsaks);
                return SigUtil.fromJavaKey(rsapub, type);
            case EdDSA:
                EdDSAPrivateKey epriv = SigUtil.toJavaEdDSAKey(priv);
                EdDSAPublicKey epub = new EdDSAPublicKey(new EdDSAPublicKeySpec(epriv.getA(), epriv.getParams()));
                return SigUtil.fromJavaKey(epub, type);
            default:
                throw new IllegalArgumentException("Unsupported algorithm");
        }
    } catch (GeneralSecurityException gse) {
        throw new IllegalArgumentException("Conversion failed", gse);
    }
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) NativeBigInteger(net.i2p.util.NativeBigInteger) GeneralSecurityException(java.security.GeneralSecurityException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) ECPublicKey(java.security.interfaces.ECPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) EllipticCurve(java.security.spec.EllipticCurve) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Aggregations

EdDSAPublicKeySpec (net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec)9 PublicKey (java.security.PublicKey)6 Test (org.junit.Test)6 Signature (java.security.Signature)5 KeyPair (java.security.KeyPair)2 EdDSAPrivateKey (net.i2p.crypto.eddsa.EdDSAPrivateKey)2 EdDSAPublicKey (net.i2p.crypto.eddsa.EdDSAPublicKey)2 EdDSAPrivateKeySpec (net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec)2 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 Path (java.nio.file.Path)1 GeneralSecurityException (java.security.GeneralSecurityException)1 KeyFactory (java.security.KeyFactory)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ECPrivateKey (java.security.interfaces.ECPrivateKey)1 ECPublicKey (java.security.interfaces.ECPublicKey)1 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 ECParameterSpec (java.security.spec.ECParameterSpec)1 ECPoint (java.security.spec.ECPoint)1