Search in sources :

Example 6 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project XobotOS by xamarin.

the class ECUtil method generatePrivateKeyParameter.

public static AsymmetricKeyParameter generatePrivateKeyParameter(PrivateKey key) throws InvalidKeyException {
    if (key instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) key;
        ECParameterSpec s = k.getParameters();
        if (s == null) {
            s = ProviderUtil.getEcImplicitlyCa();
        }
        return new ECPrivateKeyParameters(k.getD(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    }
    throw new InvalidKeyException("can't identify EC private key.");
}
Also used : ECPrivateKey(org.bouncycastle.jce.interfaces.ECPrivateKey) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) InvalidKeyException(java.security.InvalidKeyException)

Example 7 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project web3sdk by FISCO-BCOS.

the class Sign method sign.

private static ECDSASignature sign(byte[] transactionHash, BigInteger privateKey) {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, CURVE);
    signer.init(true, privKey);
    BigInteger[] components = signer.generateSignature(transactionHash);
    return new ECDSASignature(components[0], components[1]).toCanonicalised();
}
Also used : HMacDSAKCalculator(org.bouncycastle.crypto.signers.HMacDSAKCalculator) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ECDSASigner(org.bouncycastle.crypto.signers.ECDSASigner) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) BigInteger(java.math.BigInteger)

Example 8 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project nuls by nuls-io.

the class Cipher method initEnc.

public ECPoint initEnc(SM2 sm2, ECPoint userKey) {
    AsymmetricCipherKeyPair key = sm2.ecc_key_pair_generator.generateKeyPair();
    ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters) key.getPrivate();
    ECPublicKeyParameters ecpub = (ECPublicKeyParameters) key.getPublic();
    BigInteger k = ecpriv.getD();
    ECPoint c1 = ecpub.getQ();
    this.p2 = userKey.multiply(k);
    reset();
    return c1;
}
Also used : ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) BigInteger(java.math.BigInteger) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair)

Example 9 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project xipki by xipki.

the class DSAPlainDigestSigner method init.

@Override
public void init(boolean forSigning, CipherParameters parameters) {
    this.forSigning = forSigning;
    AsymmetricKeyParameter param;
    if (parameters instanceof ParametersWithRandom) {
        param = (AsymmetricKeyParameter) ((ParametersWithRandom) parameters).getParameters();
    } else {
        param = (AsymmetricKeyParameter) parameters;
    }
    ParamUtil.requireNonNull("param", param);
    if (param instanceof ECPublicKeyParameters) {
        keyBitLen = ((ECPublicKeyParameters) param).getParameters().getCurve().getFieldSize();
    } else if (param instanceof ECPrivateKeyParameters) {
        keyBitLen = ((ECPrivateKeyParameters) param).getParameters().getCurve().getFieldSize();
    } else if (param instanceof DSAPublicKeyParameters) {
        keyBitLen = ((DSAPublicKeyParameters) param).getParameters().getQ().bitLength();
    } else if (param instanceof DSAPrivateKeyParameters) {
        keyBitLen = ((DSAPrivateKeyParameters) param).getParameters().getQ().bitLength();
    } else {
        throw new IllegalArgumentException("unknown parameters: " + param.getClass().getName());
    }
    if (forSigning && !param.isPrivate()) {
        throw new IllegalArgumentException("Signing Requires Private Key.");
    }
    if (!forSigning && param.isPrivate()) {
        throw new IllegalArgumentException("Verification Requires Public Key.");
    }
    reset();
    dsaSigner.init(forSigning, parameters);
}
Also used : DSAPublicKeyParameters(org.bouncycastle.crypto.params.DSAPublicKeyParameters) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) DSAPrivateKeyParameters(org.bouncycastle.crypto.params.DSAPrivateKeyParameters) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters)

Example 10 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project jruby-openssl by jruby.

the class PKeyEC method dsa_sign_asn1.

@JRubyMethod(name = "dsa_sign_asn1")
public IRubyObject dsa_sign_asn1(final ThreadContext context, final IRubyObject data) {
    try {
        ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec(getCurveName());
        ASN1ObjectIdentifier oid = getCurveOID(getCurveName());
        ECNamedDomainParameters domainParams = new ECNamedDomainParameters(oid, params.getCurve(), params.getG(), params.getN(), params.getH(), params.getSeed());
        final ECDSASigner signer = new ECDSASigner();
        final ECPrivateKey privKey = (ECPrivateKey) this.privateKey;
        signer.init(true, new ECPrivateKeyParameters(privKey.getS(), domainParams));
        final byte[] message = data.convertToString().getBytes();
        // [r, s]
        BigInteger[] signature = signer.generateSignature(message);
        // final byte[] r = signature[0].toByteArray();
        // final byte[] s = signature[1].toByteArray();
        // // ASN.1 encode as: 0x30 len 0x02 rlen (r) 0x02 slen (s)
        // final int len = 1 + (1 + r.length) + 1 + (1 + s.length);
        // 
        // final byte[] encoded = new byte[1 + 1 + len]; int i;
        // encoded[0] = 0x30;
        // encoded[1] = (byte) len;
        // encoded[2] = 0x20;
        // encoded[3] = (byte) r.length;
        // System.arraycopy(r, 0, encoded, i = 4, r.length); i += r.length;
        // encoded[i++] = 0x20;
        // encoded[i++] = (byte) s.length;
        // System.arraycopy(s, 0, encoded, i, s.length);
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        ASN1OutputStream asn1 = new ASN1OutputStream(bytes);
        ASN1EncodableVector v = new ASN1EncodableVector();
        // r
        v.add(new ASN1Integer(signature[0]));
        // s
        v.add(new ASN1Integer(signature[1]));
        asn1.writeObject(new DLSequence(v));
        return StringHelper.newString(context.runtime, bytes.buffer(), bytes.size());
    } catch (IOException ex) {
        throw newECError(context.runtime, ex.toString());
    } catch (RuntimeException ex) {
        throw newECError(context.runtime, ex.toString());
    }
}
Also used : PKey.readECPrivateKey(org.jruby.ext.openssl.impl.PKey.readECPrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) ECDSASigner(org.bouncycastle.crypto.signers.ECDSASigner) ECNamedDomainParameters(org.bouncycastle.crypto.params.ECNamedDomainParameters) ByteArrayOutputStream(org.jruby.ext.openssl.util.ByteArrayOutputStream) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) IOException(java.io.IOException) ASN1OutputStream(org.bouncycastle.asn1.ASN1OutputStream) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) DLSequence(org.bouncycastle.asn1.DLSequence) ECNamedCurveParameterSpec(org.bouncycastle.jce.spec.ECNamedCurveParameterSpec) BigInteger(java.math.BigInteger) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)26 BigInteger (java.math.BigInteger)20 ECPoint (org.bouncycastle.math.ec.ECPoint)11 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)8 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)5 ECDSASigner (org.bouncycastle.crypto.signers.ECDSASigner)5 ECMultiplier (org.bouncycastle.math.ec.ECMultiplier)5 IOException (java.io.IOException)4 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)4 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)4 AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)4 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)4 HMacDSAKCalculator (org.bouncycastle.crypto.signers.HMacDSAKCalculator)4 DSAPrivateKeyParameters (org.bouncycastle.crypto.params.DSAPrivateKeyParameters)3 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)3 InvalidKeyException (java.security.InvalidKeyException)2 PrivateKey (java.security.PrivateKey)2 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)2 DHParameter (org.bouncycastle.asn1.pkcs.DHParameter)2