Search in sources :

Example 1 with EdDSAPrivateKeySpec

use of net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec 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 EdDSAPrivateKeySpec

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

the class EdDSAEngineTest method testSign.

@Test
public void testSign() 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) {
        EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(testCase.seed, spec);
        PrivateKey sKey = new EdDSAPrivateKey(privKey);
        sgr.initSign(sKey);
        sgr.update(testCase.message);
        assertThat("Test case " + testCase.caseNum + " failed", sgr.sign(), is(equalTo(testCase.sig)));
    }
}
Also used : EdDSAParameterSpec(net.i2p.crypto.eddsa.spec.EdDSAParameterSpec) PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) Test(org.junit.Test)

Example 3 with EdDSAPrivateKeySpec

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

the class EdDSAEngineTest method testSignOneShot.

@Test
public void testSignOneShot() throws Exception {
    EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
    EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
    PrivateKey sKey = new EdDSAPrivateKey(privKey);
    sgr.initSign(sKey);
    assertThat("signOneShot() failed", sgr.signOneShot(TEST_MSG), is(equalTo(TEST_MSG_SIG)));
}
Also used : EdDSAParameterSpec(net.i2p.crypto.eddsa.spec.EdDSAParameterSpec) PrivateKey(java.security.PrivateKey) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) Test(org.junit.Test)

Example 4 with EdDSAPrivateKeySpec

use of net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec 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)

Example 5 with EdDSAPrivateKeySpec

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

the class EdDSAEngineTest method testSignResetsForReuse.

@Test
public void testSignResetsForReuse() throws Exception {
    Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
    EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
    PrivateKey sKey = new EdDSAPrivateKey(privKey);
    sgr.initSign(sKey);
    // First usage
    sgr.update(new byte[] { 0 });
    sgr.sign();
    // Second usage
    sgr.update(TEST_MSG);
    assertThat("Second sign failed", sgr.sign(), is(equalTo(TEST_MSG_SIG)));
}
Also used : EdDSAParameterSpec(net.i2p.crypto.eddsa.spec.EdDSAParameterSpec) PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) Test(org.junit.Test)

Aggregations

EdDSAPrivateKeySpec (net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec)7 PrivateKey (java.security.PrivateKey)5 EdDSAParameterSpec (net.i2p.crypto.eddsa.spec.EdDSAParameterSpec)5 Test (org.junit.Test)5 Signature (java.security.Signature)4 KeyPair (java.security.KeyPair)2 EdDSAPublicKeySpec (net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PostConstruct (javax.annotation.PostConstruct)1 EdDSAEngine (net.i2p.crypto.eddsa.EdDSAEngine)1 EdDSAPrivateKey (net.i2p.crypto.eddsa.EdDSAPrivateKey)1 EdDSAPublicKey (net.i2p.crypto.eddsa.EdDSAPublicKey)1 KeyPairGenerator (net.i2p.crypto.eddsa.KeyPairGenerator)1