Search in sources :

Example 41 with RSAKeyGenParameterSpec

use of java.security.spec.RSAKeyGenParameterSpec in project poi by apache.

the class PkiTestUtils method generateKeyPair.

static KeyPair generateKeyPair() throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    SecureRandom random = new SecureRandom();
    keyPairGenerator.initialize(new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4), random);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    return keyPair;
}
Also used : KeyPair(java.security.KeyPair) SecureRandom(java.security.SecureRandom) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator)

Example 42 with RSAKeyGenParameterSpec

use of java.security.spec.RSAKeyGenParameterSpec in project Bytecoder by mirkosertic.

the class RSAKeyPairGenerator method initialize.

// second initialize method. See JCA doc.
public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (params instanceof RSAKeyGenParameterSpec == false) {
        throw new InvalidAlgorithmParameterException("Params must be instance of RSAKeyGenParameterSpec");
    }
    RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec) params;
    int tmpKeySize = rsaSpec.getKeysize();
    BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();
    if (tmpPublicExponent == null) {
        tmpPublicExponent = RSAKeyGenParameterSpec.F4;
    } else {
        if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
            throw new InvalidAlgorithmParameterException("Public exponent must be 3 or larger");
        }
        if (tmpPublicExponent.bitLength() > tmpKeySize) {
            throw new InvalidAlgorithmParameterException("Public exponent must be smaller than key size");
        }
    }
    // do not allow unreasonably large key sizes, probably user error
    try {
        RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent, 512, 64 * 1024);
    } catch (InvalidKeyException e) {
        throw new InvalidAlgorithmParameterException("Invalid key sizes", e);
    }
    this.keySize = tmpKeySize;
    this.publicExponent = tmpPublicExponent;
    this.random = random;
}
Also used : RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger)

Example 43 with RSAKeyGenParameterSpec

use of java.security.spec.RSAKeyGenParameterSpec 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 44 with RSAKeyGenParameterSpec

use of java.security.spec.RSAKeyGenParameterSpec 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)

Example 45 with RSAKeyGenParameterSpec

use of java.security.spec.RSAKeyGenParameterSpec in project jruby-openssl by jruby.

the class PKeyRSA method rsaGenerate.

/*
     * c: rsa_generate
     */
private static PKeyRSA rsaGenerate(final Ruby runtime, PKeyRSA rsa, int keySize, BigInteger exp) throws RaiseException {
    try {
        KeyPairGenerator gen = SecurityHelper.getKeyPairGenerator("RSA");
        if ("IBMJCEFIPS".equals(gen.getProvider().getName())) {
            // IBMJCEFIPS does not support parameters
            gen.initialize(keySize);
        } else {
            gen.initialize(new RSAKeyGenParameterSpec(keySize, exp), getSecureRandom(runtime));
        }
        KeyPair pair = gen.generateKeyPair();
        rsa.privateKey = (RSAPrivateCrtKey) pair.getPrivate();
        rsa.publicKey = (RSAPublicKey) pair.getPublic();
    } catch (NoSuchAlgorithmException e) {
        throw newRSAError(runtime, e.getMessage());
    } catch (InvalidAlgorithmParameterException e) {
        throw newRSAError(runtime, e.getMessage());
    } catch (RuntimeException e) {
        throw newRSAError(rsa.getRuntime(), e);
    }
    return rsa;
}
Also used : KeyPair(java.security.KeyPair) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)48 BigInteger (java.math.BigInteger)22 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)19 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)14 KeyPair (java.security.KeyPair)14 KeyPairGenerator (java.security.KeyPairGenerator)12 RSAPublicKey (java.security.interfaces.RSAPublicKey)12 ECPublicKey (java.security.interfaces.ECPublicKey)7 PublicKey (java.security.PublicKey)6 KeyPairGeneratorSpec (android.security.KeyPairGeneratorSpec)5 ExportResult (android.security.keymaster.ExportResult)5 ASN1Integer (com.android.org.bouncycastle.asn1.ASN1Integer)5 DERBitString (com.android.org.bouncycastle.asn1.DERBitString)5 DERInteger (com.android.org.bouncycastle.asn1.DERInteger)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InvalidKeyException (java.security.InvalidKeyException)5 PrivateKey (java.security.PrivateKey)5 ProviderException (java.security.ProviderException)5 Certificate (java.security.cert.Certificate)5 CertificateFactory (java.security.cert.CertificateFactory)5