Search in sources :

Example 1 with DERSequenceGenerator

use of org.bouncycastle.asn1.DERSequenceGenerator in project keycloak by keycloak.

the class ECDSASignatureProvider method concatenatedRSToASN1DER.

public static byte[] concatenatedRSToASN1DER(final byte[] signature, int signLength) throws IOException {
    int len = signLength / 2;
    int arraySize = len + 1;
    byte[] r = new byte[arraySize];
    byte[] s = new byte[arraySize];
    System.arraycopy(signature, 0, r, 1, len);
    System.arraycopy(signature, len, s, 1, len);
    BigInteger rBigInteger = new BigInteger(r);
    BigInteger sBigInteger = new BigInteger(s);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DERSequenceGenerator seqGen = new DERSequenceGenerator(bos);
    seqGen.addObject(new ASN1Integer(rBigInteger.toByteArray()));
    seqGen.addObject(new ASN1Integer(sBigInteger.toByteArray()));
    seqGen.close();
    bos.close();
    return bos.toByteArray();
}
Also used : BigInteger(java.math.BigInteger) DERSequenceGenerator(org.bouncycastle.asn1.DERSequenceGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1Integer(org.bouncycastle.asn1.ASN1Integer)

Example 2 with DERSequenceGenerator

use of org.bouncycastle.asn1.DERSequenceGenerator in project fabric-sdk-java by hyperledger.

the class CryptoPrimitives method ecdsaSignToBytes.

/**
 * Sign data with the specified elliptic curve private key.
 *
 * @param privateKey elliptic curve private key.
 * @param data       data to sign
 * @return the signed data.
 * @throws CryptoException
 */
private byte[] ecdsaSignToBytes(ECPrivateKey privateKey, byte[] data) throws CryptoException {
    try {
        X9ECParameters params = ECNamedCurveTable.getByName(curveName);
        BigInteger curveN = params.getN();
        Signature sig = SECURITY_PROVIDER == null ? Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM) : Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM, SECURITY_PROVIDER);
        sig.initSign(privateKey);
        sig.update(data);
        byte[] signature = sig.sign();
        BigInteger[] sigs = decodeECDSASignature(signature);
        sigs = preventMalleability(sigs, curveN);
        ByteArrayOutputStream s = new ByteArrayOutputStream();
        DERSequenceGenerator seq = new DERSequenceGenerator(s);
        seq.addObject(new ASN1Integer(sigs[0]));
        seq.addObject(new ASN1Integer(sigs[1]));
        seq.close();
        return s.toByteArray();
    } catch (Exception e) {
        throw new CryptoException("Could not sign the message using private key", e);
    }
}
Also used : X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) Signature(java.security.Signature) BigInteger(java.math.BigInteger) DERSequenceGenerator(org.bouncycastle.asn1.DERSequenceGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KeyStoreException(java.security.KeyStoreException) CertPathValidatorException(java.security.cert.CertPathValidatorException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Example 3 with DERSequenceGenerator

use of org.bouncycastle.asn1.DERSequenceGenerator in project fabric-sdk-java by hyperledger.

the class SDKUtils method calculateBlockHash.

/**
 * used asn1 and get hash
 *
 * @param blockNumber
 * @param previousHash
 * @param dataHash
 * @return byte[]
 * @throws IOException
 * @throws InvalidArgumentException
 */
public static byte[] calculateBlockHash(HFClient client, long blockNumber, byte[] previousHash, byte[] dataHash) throws IOException, InvalidArgumentException {
    if (previousHash == null) {
        throw new InvalidArgumentException("previousHash parameter is null.");
    }
    if (dataHash == null) {
        throw new InvalidArgumentException("dataHash parameter is null.");
    }
    if (null == client) {
        throw new InvalidArgumentException("client parameter is null.");
    }
    CryptoSuite cryptoSuite = client.getCryptoSuite();
    if (null == client) {
        throw new InvalidArgumentException("Client crypto suite has not  been set.");
    }
    ByteArrayOutputStream s = new ByteArrayOutputStream();
    DERSequenceGenerator seq = new DERSequenceGenerator(s);
    seq.addObject(new ASN1Integer(blockNumber));
    seq.addObject(new DEROctetString(previousHash));
    seq.addObject(new DEROctetString(dataHash));
    seq.close();
    return cryptoSuite.hash(s.toByteArray());
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) CryptoSuite(org.hyperledger.fabric.sdk.security.CryptoSuite) DERSequenceGenerator(org.bouncycastle.asn1.DERSequenceGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Example 4 with DERSequenceGenerator

use of org.bouncycastle.asn1.DERSequenceGenerator 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)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)4 DERSequenceGenerator (org.bouncycastle.asn1.DERSequenceGenerator)4 BigInteger (java.math.BigInteger)3 IOException (java.io.IOException)2 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)2 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Signature (java.security.Signature)1 SignatureException (java.security.SignatureException)1 CertPathValidatorException (java.security.cert.CertPathValidatorException)1 CertificateException (java.security.cert.CertificateException)1 DEROctetString (org.bouncycastle.asn1.DEROctetString)1 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)1 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)1 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)1 ECDSASigner (org.bouncycastle.crypto.signers.ECDSASigner)1