Search in sources :

Example 1 with EdDSAEngine

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

the class SignatureManager method build.

@PostConstruct
public void build() {
    try {
        signEngine = new EdDSAEngine(MessageDigest.getInstance(keyMgr.getSpecs().getHashAlgorithm()));
        signEngine.initSign(keyMgr.getPrivateKey(keyMgr.getCurrentIndex()));
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException(e);
    }
}
Also used : EdDSAEngine(net.i2p.crypto.eddsa.EdDSAEngine) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) PostConstruct(javax.annotation.PostConstruct)

Example 2 with EdDSAEngine

use of net.i2p.crypto.eddsa.EdDSAEngine 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 3 with EdDSAEngine

use of net.i2p.crypto.eddsa.EdDSAEngine in project i2p.i2p by i2p.

the class DSAEngine method altVerifySig.

/**
 *  Generic verify any type.
 *
 *  @throws GeneralSecurityException if algorithm unvailable or on other errors
 *  @since 0.9.9 added off/len 0.9.12
 */
private boolean altVerifySig(Signature signature, byte[] data, int offset, int len, SigningPublicKey verifyingKey) throws GeneralSecurityException {
    SigType type = signature.getType();
    if (type != verifyingKey.getType())
        throw new IllegalArgumentException("type mismatch sig=" + type + " key=" + verifyingKey.getType());
    if (type == SigType.DSA_SHA1)
        return altVerifySigSHA1(signature, data, offset, len, verifyingKey);
    PublicKey pubKey = SigUtil.toJavaKey(verifyingKey);
    byte[] sigbytes = SigUtil.toJavaSig(signature);
    boolean rv;
    if (type.getBaseAlgorithm() == SigAlgo.EdDSA) {
        // take advantage of one-shot mode
        EdDSAEngine jsig = new EdDSAEngine(type.getDigestInstance());
        jsig.initVerify(pubKey);
        rv = jsig.verifyOneShot(data, offset, len, sigbytes);
    } else {
        java.security.Signature jsig = java.security.Signature.getInstance(type.getAlgorithmName());
        jsig.initVerify(pubKey);
        jsig.update(data, offset, len);
        rv = jsig.verify(sigbytes);
    }
    return rv;
}
Also used : EdDSAEngine(net.i2p.crypto.eddsa.EdDSAEngine) PublicKey(java.security.PublicKey) SigningPublicKey(net.i2p.data.SigningPublicKey)

Example 4 with EdDSAEngine

use of net.i2p.crypto.eddsa.EdDSAEngine in project i2p.i2p by i2p.

the class DSAEngine method altSignRaw.

/**
 *  Generic raw sign any type.
 *
 *  Warning, nonstandard for EdDSA, double-hashes, not recommended.
 *
 *  @param hash SHA1Hash, Hash, Hash384, or Hash512
 *  @param type returns a Signature of this type
 *  @throws GeneralSecurityException if algorithm unvailable or on other errors
 *  @since 0.9.9
 */
private Signature altSignRaw(String algo, SimpleDataStructure hash, PrivateKey privKey, SigType type) throws GeneralSecurityException {
    int hashlen = hash.length();
    if (type.getHashLen() != hashlen)
        throw new IllegalArgumentException("type mismatch hash=" + hash.getClass() + " key=" + type);
    byte[] sigbytes;
    if (type.getBaseAlgorithm() == SigAlgo.EdDSA) {
        // take advantage of one-shot mode
        // Ignore algo, EdDSAKey includes a hash specification.
        EdDSAEngine jsig = new EdDSAEngine();
        jsig.initSign(privKey);
        sigbytes = jsig.signOneShot(hash.getData());
    } else {
        java.security.Signature jsig = java.security.Signature.getInstance(algo);
        jsig.initSign(privKey, _context.random());
        jsig.update(hash.getData());
        sigbytes = jsig.sign();
    }
    return SigUtil.fromJavaSig(sigbytes, type);
}
Also used : EdDSAEngine(net.i2p.crypto.eddsa.EdDSAEngine)

Example 5 with EdDSAEngine

use of net.i2p.crypto.eddsa.EdDSAEngine in project i2p.i2p by i2p.

the class DSAEngine method altVerifySigRaw.

/**
 *  Generic raw verify any type.
 *  If you have a Java pubkey, use this, so you don't lose the key parameters,
 *  which may be different than the ones defined in SigType.
 *
 *  Warning, nonstandard for EdDSA, double-hashes, not recommended.
 *
 *  @throws GeneralSecurityException if algorithm unvailable or on other errors
 *  @param verifyingKey Java key
 *  @since 0.9.9
 */
private boolean altVerifySigRaw(Signature signature, SimpleDataStructure hash, PublicKey pubKey) throws GeneralSecurityException {
    SigType type = signature.getType();
    int hashlen = hash.length();
    if (type.getHashLen() != hashlen)
        throw new IllegalArgumentException("type mismatch hash=" + hash.getClass() + " key=" + type);
    byte[] sigbytes = SigUtil.toJavaSig(signature);
    boolean rv;
    if (type.getBaseAlgorithm() == SigAlgo.EdDSA) {
        // take advantage of one-shot mode
        // Ignore algo, EdDSAKey includes a hash specification.
        EdDSAEngine jsig = new EdDSAEngine();
        jsig.initVerify(pubKey);
        rv = jsig.verifyOneShot(hash.getData(), sigbytes);
    } else {
        String algo = getRawAlgo(type);
        java.security.Signature jsig = java.security.Signature.getInstance(algo);
        jsig.initVerify(pubKey);
        jsig.update(hash.getData());
        rv = jsig.verify(sigbytes);
    }
    return rv;
}
Also used : EdDSAEngine(net.i2p.crypto.eddsa.EdDSAEngine)

Aggregations

EdDSAEngine (net.i2p.crypto.eddsa.EdDSAEngine)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 PostConstruct (javax.annotation.PostConstruct)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyPair (java.security.KeyPair)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 EdDSAPrivateKey (net.i2p.crypto.eddsa.EdDSAPrivateKey)1 EdDSAPublicKey (net.i2p.crypto.eddsa.EdDSAPublicKey)1 KeyPairGenerator (net.i2p.crypto.eddsa.KeyPairGenerator)1 EdDSAPrivateKeySpec (net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec)1 EdDSAPublicKeySpec (net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec)1 SigningPrivateKey (net.i2p.data.SigningPrivateKey)1 SigningPublicKey (net.i2p.data.SigningPublicKey)1