Search in sources :

Example 11 with ECDSASigner

use of org.spongycastle.crypto.signers.ECDSASigner in project java-tron by tronprotocol.

the class ECKey method verify.

/**
 * <p>Verifies the given ECDSA signature against the message bytes using the public key bytes.</p>
 * <p> <p>When using native ECDSA verification, data must be 32 bytes, and no element may be
 * larger than 520 bytes.</p>
 *
 * @param data Hash of the data to verify.
 * @param signature signature.
 * @param pub The public key bytes to use.
 * @return -
 */
public static boolean verify(byte[] data, ECDSASignature signature, byte[] pub) {
    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
    signer.init(false, params);
    try {
        return signer.verifySignature(data, signature.r, signature.s);
    } catch (NullPointerException npe) {
        // Bouncy Castle contains a bug that can cause NPEs given
        // specially crafted signatures.
        // Those signatures are inherently invalid/attack sigs so we just
        // fail them here rather than crash the thread.
        logger.error("Caught NPE inside bouncy castle", npe);
        return false;
    }
}
Also used : ECDSASigner(org.spongycastle.crypto.signers.ECDSASigner) ECPublicKeyParameters(org.spongycastle.crypto.params.ECPublicKeyParameters)

Example 12 with ECDSASigner

use of org.spongycastle.crypto.signers.ECDSASigner in project toshi-android-client by toshiapp.

the class ECKey method doSign.

/**
 * Signs the given hash and returns the R and S components as BigIntegers
 * and put them in ECDSASignature
 *
 * @param input to sign
 * @return ECDSASignature signature that contains the R and S components
 */
public ECDSASignature doSign(byte[] input) {
    if (input.length != 32) {
        throw new IllegalArgumentException("Expected 32 byte input to ECDSA signature, not " + input.length);
    }
    // No decryption of private key required.
    if (privKey == null)
        throw new MissingPrivateKeyException();
    if (privKey instanceof BCECPrivateKey) {
        ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
        ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters(((BCECPrivateKey) privKey).getD(), CURVE);
        signer.init(true, privKeyParams);
        BigInteger[] components = signer.generateSignature(input);
        return new ECDSASignature(components[0], components[1]).toCanonicalised();
    } else {
        try {
            final Signature ecSig = ECSignatureFactory.getRawInstance(provider);
            ecSig.initSign(privKey);
            ecSig.update(input);
            final byte[] derSignature = ecSig.sign();
            return ECDSASignature.decodeFromDER(derSignature).toCanonicalised();
        } catch (SignatureException | InvalidKeyException ex) {
            throw new RuntimeException("ECKey signing error", ex);
        }
    }
}
Also used : ECDSASigner(org.spongycastle.crypto.signers.ECDSASigner) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) BCECPrivateKey(org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey) HMacDSAKCalculator(org.spongycastle.crypto.signers.HMacDSAKCalculator) ECPrivateKeyParameters(org.spongycastle.crypto.params.ECPrivateKeyParameters) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) Signature(java.security.Signature) BigInteger(java.math.BigInteger)

Example 13 with ECDSASigner

use of org.spongycastle.crypto.signers.ECDSASigner in project toshi-android-client by toshiapp.

the class ECKey method verify.

/**
 * <p>Verifies the given ECDSA signature against the message bytes using the public key bytes.</p>
 *
 * <p>When using native ECDSA verification, data must be 32 bytes, and no element may be
 * larger than 520 bytes.</p>
 *
 * @param data Hash of the data to verify.
 * @param signature signature.
 * @param pub The public key bytes to use.
 *
 * @return -
 */
public static boolean verify(byte[] data, ECDSASignature signature, byte[] pub) {
    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
    signer.init(false, params);
    try {
        return signer.verifySignature(data, signature.r, signature.s);
    } catch (NullPointerException npe) {
        return false;
    }
}
Also used : ECDSASigner(org.spongycastle.crypto.signers.ECDSASigner) ECPublicKeyParameters(org.spongycastle.crypto.params.ECPublicKeyParameters)

Example 14 with ECDSASigner

use of org.spongycastle.crypto.signers.ECDSASigner in project rskj by rsksmart.

the class ECKey method verify.

/**
 * <p>Verifies the given ECDSA signature against the message bytes using the public key bytes.</p>
 *
 * <p>When using native ECDSA verification, data must be 32 bytes, and no element may be
 * larger than 520 bytes.</p>
 *
 * @param data Hash of the data to verify.
 * @param signature signature.
 * @param pub The public key bytes to use.
 *
 * @return -
 */
public static boolean verify(byte[] data, ECDSASignature signature, byte[] pub) {
    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
    signer.init(false, params);
    try {
        return signer.verifySignature(data, signature.r, signature.s);
    } catch (NullPointerException npe) {
        // Bouncy Castle contains a bug that can cause NPEs given specially crafted signatures.
        // Those signatures are inherently invalid/attack sigs so we just fail them here rather than crash the thread.
        logger.error("Caught NPE inside bouncy castle", npe);
        return false;
    }
}
Also used : ECDSASigner(org.spongycastle.crypto.signers.ECDSASigner)

Aggregations

ECDSASigner (org.spongycastle.crypto.signers.ECDSASigner)14 BigInteger (java.math.BigInteger)8 SHA256Digest (org.spongycastle.crypto.digests.SHA256Digest)7 HMacDSAKCalculator (org.spongycastle.crypto.signers.HMacDSAKCalculator)7 ECPrivateKeyParameters (org.spongycastle.crypto.params.ECPrivateKeyParameters)6 InvalidKeyException (java.security.InvalidKeyException)4 Signature (java.security.Signature)4 SignatureException (java.security.SignatureException)4 ECPublicKeyParameters (org.spongycastle.crypto.params.ECPublicKeyParameters)4 BCECPrivateKey (org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)4 Script (co.rsk.bitcoinj.script.Script)1 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)1 Keccak256 (co.rsk.crypto.Keccak256)1 RepositoryImpl (co.rsk.db.RepositoryImpl)1 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)1 BridgeEventLoggerImpl (co.rsk.peg.utils.BridgeEventLoggerImpl)1 ISignature (org.aion.crypto.ISignature)1 RLPList (org.ethereum.util.RLPList)1 LogInfo (org.ethereum.vm.LogInfo)1