Search in sources :

Example 1 with EdDSAPublicKey

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

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

the class SigUtil method toJavaEdDSAKey.

/**
 *  @return JAVA EdDSA public key!
 *  @since 0.9.15
 */
public static EdDSAPublicKey toJavaEdDSAKey(SigningPublicKey pk) throws GeneralSecurityException {
    EdDSAPublicKey rv;
    synchronized (_EdPubkeyCache) {
        rv = _EdPubkeyCache.get(pk);
    }
    if (rv != null)
        return rv;
    rv = cvtToJavaEdDSAKey(pk);
    synchronized (_EdPubkeyCache) {
        _EdPubkeyCache.put(pk, rv);
    }
    return rv;
}
Also used : EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey)

Example 3 with EdDSAPublicKey

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

the class SigUtil method fromJavaKey.

/**
 *  Use if SigType is unknown.
 *  For efficiency, use fromJavakey(pk, type) if type is known.
 *
 *  @param pk JAVA key!
 *  @throws IllegalArgumentException on unknown type
 *  @since 0.9.18
 */
public static SigningPublicKey fromJavaKey(PublicKey pk) throws GeneralSecurityException {
    if (pk instanceof DSAPublicKey) {
        return fromJavaKey((DSAPublicKey) pk);
    }
    if (pk instanceof ECPublicKey) {
        ECPublicKey k = (ECPublicKey) pk;
        AlgorithmParameterSpec spec = k.getParams();
        SigType type;
        if (spec.equals(SigType.ECDSA_SHA256_P256.getParams()))
            type = SigType.ECDSA_SHA256_P256;
        else if (spec.equals(SigType.ECDSA_SHA384_P384.getParams()))
            type = SigType.ECDSA_SHA384_P384;
        else if (spec.equals(SigType.ECDSA_SHA512_P521.getParams()))
            type = SigType.ECDSA_SHA512_P521;
        else
            throw new IllegalArgumentException("Unknown EC type");
        return fromJavaKey(k, type);
    }
    if (pk instanceof EdDSAPublicKey) {
        return fromJavaKey((EdDSAPublicKey) pk, SigType.EdDSA_SHA512_Ed25519);
    }
    if (pk instanceof RSAPublicKey) {
        RSAPublicKey k = (RSAPublicKey) pk;
        int sz = k.getModulus().bitLength();
        SigType type;
        if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA256_2048.getParams()).getKeysize())
            type = SigType.RSA_SHA256_2048;
        else if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA384_3072.getParams()).getKeysize())
            type = SigType.RSA_SHA384_3072;
        else if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA512_4096.getParams()).getKeysize())
            type = SigType.RSA_SHA512_4096;
        else
            throw new IllegalArgumentException("Unknown RSA type");
        return fromJavaKey(k, type);
    }
    throw new IllegalArgumentException("Unknown type: " + pk.getClass());
}
Also used : EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) ECPoint(java.security.spec.ECPoint) DSAPublicKey(java.security.interfaces.DSAPublicKey) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey)

Example 4 with EdDSAPublicKey

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

EdDSAPublicKey (net.i2p.crypto.eddsa.EdDSAPublicKey)4 ECPublicKey (java.security.interfaces.ECPublicKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 ECPoint (java.security.spec.ECPoint)2 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)2 EdDSAPrivateKey (net.i2p.crypto.eddsa.EdDSAPrivateKey)2 EdDSAPublicKeySpec (net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec)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 KeyPair (java.security.KeyPair)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 DSAPublicKey (java.security.interfaces.DSAPublicKey)1 ECPrivateKey (java.security.interfaces.ECPrivateKey)1 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1 ECParameterSpec (java.security.spec.ECParameterSpec)1 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)1