Search in sources :

Example 16 with ECDomainParameters

use of org.bouncycastle.crypto.params.ECDomainParameters in project OsmAnd-tools by osmandapp.

the class SigningUtils method signData.

static String signData(String input, byte[] key) throws BlockIOException {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    X9ECParameters params = SECNamedCurves.getByName("secp256k1");
    ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH());
    BigInteger priv = new BigInteger(1, key);
    ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(priv, ecParams);
    signer.init(true, privKey);
    BigInteger[] sigs = signer.generateSignature(fromHex(input));
    BigInteger r = sigs[0];
    BigInteger s = sigs[1];
    // BIP62: "S must be less than or equal to half of the Group Order N"
    BigInteger overTwo = params.getN().shiftRight(1);
    if (s.compareTo(overTwo) == 1) {
        s = params.getN().subtract(s);
    }
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DERSequenceGenerator seq = new DERSequenceGenerator(bos);
        seq.addObject(new ASN1Integer(r));
        seq.addObject(new ASN1Integer(s));
        seq.close();
        return toHex(bos.toByteArray());
    } catch (IOException e) {
        // Cannot happen.
        throw new BlockIOException("That should never happen... File an issue report.");
    }
}
Also used : ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECDSASigner(org.bouncycastle.crypto.signers.ECDSASigner) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) IOException(java.io.IOException) HMacDSAKCalculator(org.bouncycastle.crypto.signers.HMacDSAKCalculator) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) BigInteger(java.math.BigInteger) DERSequenceGenerator(org.bouncycastle.asn1.DERSequenceGenerator)

Example 17 with ECDomainParameters

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

the class PKCS10Request method generatePublicKey.

public PublicKey generatePublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    AsymmetricKeyParameter keyParams = PublicKeyFactory.createKey(publicKeyInfo);
    final KeySpec keySpec;
    final KeyFactory keyFactory;
    if (keyParams instanceof RSAKeyParameters) {
        RSAKeyParameters rsa = (RSAKeyParameters) keyParams;
        keySpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
        keyFactory = SecurityHelper.getKeyFactory("RSA");
        return keyFactory.generatePublic(keySpec);
    } else if (keyParams instanceof DSAPublicKeyParameters) {
        DSAPublicKeyParameters dsa = (DSAPublicKeyParameters) keyParams;
        DSAParameters params = dsa.getParameters();
        keySpec = new DSAPublicKeySpec(dsa.getY(), params.getP(), params.getQ(), params.getG());
        keyFactory = SecurityHelper.getKeyFactory("DSA");
        return keyFactory.generatePublic(keySpec);
    } else if (keyParams instanceof ECPublicKeyParameters) {
        ECPublicKeyParameters ec = (ECPublicKeyParameters) keyParams;
        ECDomainParameters ecParams = ec.getParameters();
        ECParameterSpec params = new ECParameterSpec(ecParams.getCurve(), ecParams.getG(), ecParams.getN(), ecParams.getH(), ecParams.getSeed());
        // NOTE: likely to fail if non BC factory picked up :
        keySpec = new ECPublicKeySpec(ec.getQ(), params);
        keyFactory = SecurityHelper.getKeyFactory("EC");
        return keyFactory.generatePublic(keySpec);
    } else {
        throw new IllegalStateException("could not generate public key for request, params type: " + keyParams);
    }
}
Also used : DSAPublicKeyParameters(org.bouncycastle.crypto.params.DSAPublicKeyParameters) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters) RSAKeyParameters(org.bouncycastle.crypto.params.RSAKeyParameters) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) DSAParameters(org.bouncycastle.crypto.params.DSAParameters) PublicKeyFactory(org.bouncycastle.crypto.util.PublicKeyFactory) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)17 BigInteger (java.math.BigInteger)11 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)8 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)8 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)7 DSAParameters (org.bouncycastle.crypto.params.DSAParameters)5 ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)5 InvalidKeyException (java.security.InvalidKeyException)4 DHParameter (org.bouncycastle.asn1.pkcs.DHParameter)4 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)4 DSAParameter (org.bouncycastle.asn1.x509.DSAParameter)4 X962Parameters (org.bouncycastle.asn1.x9.X962Parameters)4 DHParameters (org.bouncycastle.crypto.params.DHParameters)4 ECPoint (org.bouncycastle.math.ec.ECPoint)4 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)3 DSAPublicKeyParameters (org.bouncycastle.crypto.params.DSAPublicKeyParameters)3 RSAKeyParameters (org.bouncycastle.crypto.params.RSAKeyParameters)3 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)2 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)2