@Override public boolean verify(byte[] hash, byte[] signature, byte[] publicKey) { ASN1InputStream asn1 = new ASN1InputStream(signature); try { ECDSASigner signer = new ECDSASigner(); signer.init(false, new ECPublicKeyParameters(curve.getCurve().decodePoint(publicKey), domain)); DLSequence seq = (DLSequence) asn1.readObject(); BigInteger r = ((ASN1Integer) seq.getObjectAt(0)).getPositiveValue(); BigInteger s = ((ASN1Integer) seq.getObjectAt(1)).getPositiveValue(); return signer.verifySignature(hash, r, s); } catch (Exception e) { return false; } finally { try { asn1.close(); } catch (IOException ignored) { } } }
static Signer createSigner(int keyAlgorithm, int hashAlgorithm) throws PGPException { switch(keyAlgorithm) { case PublicKeyAlgorithmTags.RSA_GENERAL: case PublicKeyAlgorithmTags.RSA_SIGN: return new RSADigestSigner(createDigest(hashAlgorithm)); case PublicKeyAlgorithmTags.DSA: return new DSADigestSigner(new DSASigner(), createDigest(hashAlgorithm)); case PublicKeyAlgorithmTags.ECDSA: return new DSADigestSigner(new ECDSASigner(), createDigest(hashAlgorithm)); default: throw new PGPException("cannot recognise keyAlgorithm: " + keyAlgorithm); } }
@Override public byte[] sign(byte[] hash, byte[] privateKey) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); signer.init(true, new ECPrivateKeyParameters(new BigInteger(privateKey), domain)); BigInteger[] signature = signer.generateSignature(hash); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(signature[0])); seq.addObject(new ASN1Integer(toCanonicalS(signature[1]))); seq.close(); return baos.toByteArray(); } catch (IOException e) { return new byte[0]; } }
public static boolean verifySignature(byte[] sigR, byte sigS[], byte[] publicKey, byte[] message) { try { Security.addProvider(new BouncyCastleProvider()); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(SECP256K1); ECDomainParameters domain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN()); ECPublicKeyParameters publicKeyParams = new ECPublicKeyParameters(spec.getCurve().decodePoint(publicKey), domain); ECDSASigner signer = new ECDSASigner(); signer.init(false, publicKeyParams); return signer.verifySignature(message, new BigInteger(1, sigR), new BigInteger(1, sigS)); } catch (Exception e) { LOGGER.error(null, e); return false; } }
public static boolean verifyUsingSecp256k1(byte[] pub, byte[] dataForSigning, BigInteger[] rs) throws Exception { ECDSASigner signer = new ECDSASigner(); X9ECParameters params = SECNamedCurves.getByName("secp256k1"); ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); ECPublicKeyParameters pubKeyParams = new ECPublicKeyParameters(ecParams .getCurve().decodePoint(pub), ecParams); signer.init(false, pubKeyParams); return signer.verifySignature(dataForSigning, rs[0].abs(), rs[1].abs()); }
@Test public void curveTest() { final X9ECParameters curve = NISTNamedCurves.getByName("P-384"); final ECDomainParameters domainParameters = new ECDomainParameters( curve.getCurve(), curve.getG(), curve.getN()); final SecureRandom random = new SecureRandom(); final ECKeyPairGenerator gen = new ECKeyPairGenerator(); gen.init(new ECKeyGenerationParameters(domainParameters, random)); final AsymmetricCipherKeyPair senderPair = gen.generateKeyPair(); final ECDSASigner signer = new ECDSASigner(); signer.init(true, senderPair.getPrivate()); final byte[] message = "The magic words are squeamish ossifrage".getBytes( Constants.ASCII); final SHA384Digest digester = new SHA384Digest(); digester.update(message, 0, message.length); final byte[] digest = new byte[digester.getDigestSize()]; digester.doFinal(digest, 0); final BigInteger[] signature = signer.generateSignature(digest); final ECDSASigner verifier = new ECDSASigner(); verifier.init(false, senderPair.getPublic()); assertTrue(verifier.verifySignature(digest, signature[0], signature[1])); }
@Override public byte[] sign (byte[] hash) throws ValidationException { if ( priv == null ) { throw new ValidationException ("Need private key to sign"); } ECDSASigner signer = new ECDSASigner (new HMacDSAKCalculator (new SHA256Digest ())); signer.init (true, new ECPrivateKeyParameters (priv, domain)); BigInteger[] signature = signer.generateSignature (hash); ByteArrayOutputStream s = new ByteArrayOutputStream (); try { DERSequenceGenerator seq = new DERSequenceGenerator (s); seq.addObject (new ASN1Integer (signature[0])); seq.addObject (new ASN1Integer (signature[1])); seq.close (); return s.toByteArray (); } catch ( IOException e ) { } return null; }
public boolean isSignatureVerified(StellarBinaryObject serObj) { try { byte[] signatureBytes= (byte[]) serObj.getField(BinaryFormatField.TxnSignature); if(signatureBytes==null){ throw new RuntimeException("The specified has no signature"); } byte[] signingPubKeyBytes = (byte[]) serObj.getField(BinaryFormatField.SigningPubKey); if(signingPubKeyBytes==null){ throw new RuntimeException("The specified has no public key associated to the signature"); } StellarBinaryObject unsignedRBO = serObj.getUnsignedCopy(); byte[] hashToVerify = unsignedRBO.generateHashFromBinaryObject(); ECDSASigner signer = new ECDSASigner(); ECDSASignature signature = new ECDSASignature(signatureBytes, signingPubKeyBytes); signer.init(false, new ECPublicKeyParameters(signature.publicSigningKey, StellarDeterministicKeyGenerator.SECP256K1_PARAMS)); return signer.verifySignature(hashToVerify, signature.r, signature.s); } catch (IOException e) { throw new RuntimeException(e); } }
protected Signer createSigner(AlgorithmIdentifier sigAlgId, AlgorithmIdentifier digAlgId) throws OperatorCreationException { if (!AlgorithmUtil.isECSigAlg(sigAlgId)) { throw new OperatorCreationException( "the given algorithm is not a valid EC signature algorithm '" + sigAlgId.getAlgorithm().getId() + "'"); } Digest dig = digestProvider.get(digAlgId); ECDSASigner dsaSigner = new ECDSASigner(); return plain ? new DSAPlainDigestSigner(dsaSigner, dig) : new DSADigestSigner(dsaSigner, dig); }
default byte[] sign(Account account) { ECDSASigner signer = new ECDSASigner(); signer.init(true, new ECPrivateKeyParameters(new BigInteger(1, account.privateKey), ECC.secp256r1)); BigInteger[] bi = signer.generateSignature(Digest.sha256(getHashData()));// dna byte[] signature = new byte[64]; System.arraycopy(BigIntegers.asUnsignedByteArray(32, bi[0]), 0, signature, 0, 32); System.arraycopy(BigIntegers.asUnsignedByteArray(32, bi[1]), 0, signature, 32, 32); return signature; }
/** * UAF_ALG_SIGN_SECP256R1_ECDSA_SHA256_RAW 0x01 An ECDSA signature on the * NIST secp256r1 curve which MUST have raw R and S buffers, encoded in * big-endian order. I.e. [R (32 bytes), S (32 bytes)] * * @param priv * - Private key * @param input * - Data to sign * @return BigInteger[] - [R,S] */ public static BigInteger[] signAndFromatToRS(PrivateKey priv, byte[] input) { X9ECParameters params = SECNamedCurves.getByName("secp256r1"); ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); if (priv == null) throw new IllegalStateException( "This ECKey does not have the private key necessary for signing."); ECDSASigner signer = new ECDSASigner(); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters( ((ECPrivateKey) priv).getS(), ecParams); signer.init(true, privKey); BigInteger[] sigs = signer.generateSignature(input); return sigs; }
public static boolean verify(byte[] pub, byte[] dataForSigning, BigInteger[] rs) throws Exception { ECDSASigner signer = new ECDSASigner(); X9ECParameters params = SECNamedCurves.getByName("secp256r1"); ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); ECPublicKeyParameters pubKeyParams = new ECPublicKeyParameters(ecParams .getCurve().decodePoint(pub), ecParams); signer.init(false, pubKeyParams); return signer.verifySignature(dataForSigning, rs[0].abs(), rs[1].abs()); }
/** * Sign a hash with the private key of this key pair. * @param transactionHash the hash to sign * @return An {@link ECDSASignature} of the hash */ public ECDSASignature sign(byte[] transactionHash) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE); signer.init(true, privKey); BigInteger[] components = signer.generateSignature(transactionHash); return new ECDSASignature(components[0], components[1]).toCanonicalised(); }
/** * Sign a digest with this key. * * @param hash arbitrary data * @return signature */ public byte[] sign(byte[] hash) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); signer.init(true, new ECPrivateKeyParameters(priv, domain)); BigInteger[] signature = signer.generateSignature(hash); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(signature[0])); seq.addObject(new ASN1Integer(toCanonicalS(signature[1]))); seq.close(); return baos.toByteArray(); } catch (IOException e) { } return null; }
protected Signer createSigner(AlgorithmIdentifier sigAlgId, AlgorithmIdentifier digAlgId) throws OperatorCreationException { Digest dig = digestProvider.get(digAlgId); return new DSADigestSigner(new ECDSASigner(), dig); }
protected Signer createSigner(AlgorithmIdentifier sigAlgId) throws OperatorCreationException { AlgorithmIdentifier digAlg = digestAlgorithmFinder.find(sigAlgId); Digest dig = digestProvider.get(digAlg); return new DSADigestSigner(new ECDSASigner(), dig); }
public BigInteger[] ecdsaSign(PrivateKey privateKey, ByteString message) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(digest)); ECPrivateKeyParameters ecdhPrivateKeyParameters; try { ecdhPrivateKeyParameters = (ECPrivateKeyParameters) (PrivateKeyFactory.createKey(privateKey.getEncoded())); } catch (IOException e) { logger.error("ECDSA sign load private key exception", e); throw new RuntimeException(e); } signer.init(true, ecdhPrivateKeyParameters); return signer.generateSignature(message.toByteArray()); }
/** * Sign data using the ECDSA algorithm. */ public static byte[][] signTransaction(byte[] data, byte[] privateKey) { try { Security.addProvider(new BouncyCastleProvider()); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(SECP256K1); ECDSASigner ecdsaSigner = new ECDSASigner(); ECDomainParameters domain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN()); ECPrivateKeyParameters privateKeyParms = new ECPrivateKeyParameters(new BigInteger(1, privateKey), domain); ParametersWithRandom params = new ParametersWithRandom(privateKeyParms); ecdsaSigner.init(true, params); BigInteger[] sig = ecdsaSigner.generateSignature(data); LinkedList<byte[]> sigData = new LinkedList<>(); byte[] publicKey = getPublicKey(privateKey); byte recoveryId = getRecoveryId(sig[0].toByteArray(), sig[1].toByteArray(), data, publicKey); for (BigInteger sigChunk : sig) { sigData.add(sigChunk.toByteArray()); } sigData.add(new byte[]{recoveryId}); return sigData.toArray(new byte[][]{}); } catch (Exception e) { LOGGER.error(null, e); return new byte[0][0]; } }
public boolean verifySignature(byte[] message, BigInteger r, BigInteger s) { ECDomainParameters ecDomainParameters = ECAssistant.ecDomainParametersFrom(x9ECParameters); ECPublicKeyParameters ecPublicKeyParameters = new ECPublicKeyParameters(Q, ecDomainParameters); ECDSASigner signer = new ECDSASigner(); signer.init(false, ecPublicKeyParameters); return signer.verifySignature(message, r, s); }
public byte[] sign(byte[] message) throws Exception { if (priv == null) { throw new Exception("Unable to sign"); } ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); signer.init(true, new ECPrivateKeyParameters(priv, params)); BigInteger[] signature = signer.generateSignature(message); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DERSequenceGenerator seqGen = new DERSequenceGenerator(outputStream); seqGen.addObject(new ASN1Integer(signature[0])); seqGen.addObject(new ASN1Integer(signature[1])); seqGen.close(); return outputStream.toByteArray(); }
public boolean verify(byte[] message, byte[] signature) throws Exception { ASN1InputStream asn1 = new ASN1InputStream(signature); ECDSASigner signer = new ECDSASigner(); //not for signing... signer.init(false, new ECPublicKeyParameters(curve.getCurve().decodePoint(pub), params)); DLSequence seq = (DLSequence) asn1.readObject(); BigInteger r = ((ASN1Integer) seq.getObjectAt(0)).getPositiveValue(); BigInteger s = ((ASN1Integer) seq.getObjectAt(1)).getPositiveValue(); return signer.verifySignature(message, r, s); }
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) { throw new BlockIOException("That should never happen... File an issue report."); // Cannot happen. } }
/** * Creates a signature for the supplied contents using the private key * * @param contents Contents to be signed * @return ECDSA signature * @throws ECException Unable to create signature */ public ECDSASignature createSignature(byte[] contents) throws ECException { if (privKey == null) throw new IllegalStateException("No private key available"); // // Get the double SHA-256 hash of the signed contents // byte[] contentsHash = Utils.doubleDigest(contents); // // Create the signature // BigInteger[] sigs; try { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters(privKey, ecParams); signer.init(true, privKeyParams); sigs = signer.generateSignature(contentsHash); } catch (RuntimeException exc) { throw new ECException("Exception while creating signature", exc); } // // Create a canonical signature by adjusting the S component to be less than or equal to // half the curve order. // if (sigs[1].compareTo(HALF_CURVE_ORDER) > 0) sigs[1] = ecParams.getN().subtract(sigs[1]); return new ECDSASignature(sigs[0], sigs[1]); }
/** * Verifies a signature for the signed contents using the public key * * @param contents The signed contents * @param signature DER-encoded signature * @return TRUE if the signature if valid, FALSE otherwise * @throws ECException Unable to verify the signature */ public boolean verifySignature(byte[] contents, byte[] signature) throws ECException { boolean isValid = false; // // Decode the DER-encoded signature and get the R and S values // ECDSASignature sig = new ECDSASignature(signature); // // Get the double SHA-256 hash of the signed contents // // A null contents will result in a hash with the first byte set to 1 and // all other bytes set to 0. This is needed to handle a bug in the reference // client where it doesn't check for an error when serializing a transaction // and instead uses the error code as the hash. // byte[] contentsHash; if (contents != null) { contentsHash = Utils.doubleDigest(contents); } else { contentsHash = new byte[32]; contentsHash[0] = 0x01; } // // Verify the signature // try { ECDSASigner signer = new ECDSASigner(); ECPublicKeyParameters params = new ECPublicKeyParameters( ecParams.getCurve().decodePoint(pubKey), ecParams); signer.init(false, params); isValid = signer.verifySignature(contentsHash, sig.getR(), sig.getS()); } catch (RuntimeException exc) { throw new ECException("Exception while verifying signature: "+exc.getMessage()); } return isValid; }
/** * key generation test */ private void testECDSAKeyGenTest() { SecureRandom random = new SecureRandom(); ECCurve.Fp curve = new ECCurve.Fp( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b ECDomainParameters params = new ECDomainParameters( curve, curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters( params, random); pGen.init(genParam); AsymmetricCipherKeyPair pair = pGen.generateKeyPair(); ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random); ECDSASigner ecdsa = new ECDSASigner(); ecdsa.init(true, param); byte[] message = new BigInteger("968236873715988614170569073515315707566766479517").toByteArray(); BigInteger[] sig = ecdsa.generateSignature(message); ecdsa.init(false, pair.getPublic()); if (!ecdsa.verifySignature(message, sig[0], sig[1])) { fail("signature fails"); } }
private ECDSASignature signHash(byte[] hashOfBytes) throws Exception { if(hashOfBytes.length!=32){ throw new RuntimeException("can sign only a hash of 32 bytes"); } ECDSASigner signer = new ECDSASigner(); ECPrivateKeyParameters privKey = privateKey.getECPrivateKey(); signer.init(true, privKey); BigInteger[] RandS = signer.generateSignature(hashOfBytes); return new ECDSASignature(RandS[0], RandS[1], privateKey.getPublicKey().getPublicPoint()); }
protected DSA createDSAImpl() { return new ECDSASigner(); }
public ecDSA() { super(new SHA1Digest(), new ECDSASigner(), new StdDSAEncoder()); }
public ecDSAnone() { super(new NullDigest(), new ECDSASigner(), new StdDSAEncoder()); }
public ecDSA224() { super(new SHA224Digest(), new ECDSASigner(), new StdDSAEncoder()); }
public ecDSA256() { super(new SHA256Digest(), new ECDSASigner(), new StdDSAEncoder()); }