Search in sources :

Example 1 with EdDSAPublicKeySpec

use of net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec in project mxisd by kamax-io.

the class KeyManager method build.

@PostConstruct
public void build() {
    try {
        keySpecs = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512);
        signEngine = new EdDSAEngine(MessageDigest.getInstance(keySpecs.getHashAlgorithm()));
        keys = new ArrayList<>();
        Path privKey = Paths.get(keyCfg.getPath());
        if (!Files.exists(privKey)) {
            KeyPair pair = (new KeyPairGenerator()).generateKeyPair();
            String keyEncoded = Base64.getEncoder().encodeToString(pair.getPrivate().getEncoded());
            FileUtils.writeStringToFile(privKey.toFile(), keyEncoded, StandardCharsets.ISO_8859_1);
            keys.add(pair);
        } else {
            if (Files.isDirectory(privKey)) {
                throw new RuntimeException("Invalid path for private key: " + privKey.toString());
            }
            if (Files.isReadable(privKey)) {
                byte[] seed = Base64.getDecoder().decode(FileUtils.readFileToString(privKey.toFile(), StandardCharsets.ISO_8859_1));
                EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(seed, keySpecs);
                EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKeySpec.getA(), keySpecs);
                keys.add(new KeyPair(new EdDSAPublicKey(pubKeySpec), new EdDSAPrivateKey(privKeySpec)));
            }
        }
    } catch (NoSuchAlgorithmException | IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : EdDSAEngine(net.i2p.crypto.eddsa.EdDSAEngine) Path(java.nio.file.Path) KeyPair(java.security.KeyPair) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) KeyPairGenerator(net.i2p.crypto.eddsa.KeyPairGenerator) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) PostConstruct(javax.annotation.PostConstruct)

Example 2 with EdDSAPublicKeySpec

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

the class EdDSAEngineTest method testVerifyResetsForReuse.

@Test
public void testVerifyResetsForReuse() 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);
    // First usage
    sgr.update(new byte[] { 0 });
    sgr.verify(TEST_MSG_SIG);
    // Second usage
    sgr.update(TEST_MSG);
    assertThat("Second 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 3 with EdDSAPublicKeySpec

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

the class EdDSAEngineTest method testVerifyOneShotModeMultipleUpdates.

@Test
public void testVerifyOneShotModeMultipleUpdates() 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);
    exception.expect(SignatureException.class);
    exception.expectMessage("update() already called");
    sgr.update(TEST_MSG);
}
Also used : EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) Test(org.junit.Test)

Example 4 with EdDSAPublicKeySpec

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

the class EdDSAEngineTest method testVerifyOneShot.

@Test
public void testVerifyOneShot() throws Exception {
    EdDSAEngine 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);
    assertThat("verifyOneShot() failed", sgr.verifyOneShot(TEST_MSG, TEST_MSG_SIG), is(true));
}
Also used : EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) PublicKey(java.security.PublicKey) Test(org.junit.Test)

Example 5 with EdDSAPublicKeySpec

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

the class KeyPairGenerator method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialized)
        initialize(DEFAULT_STRENGTH, RandomSource.getInstance());
    byte[] seed = new byte[edParams.getCurve().getField().getb() / 8];
    random.nextBytes(seed);
    EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(seed, edParams);
    EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(privKey.getA(), edParams);
    return new KeyPair(new EdDSAPublicKey(pubKey), new EdDSAPrivateKey(privKey));
}
Also used : KeyPair(java.security.KeyPair) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec)

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