@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) { } } }
public JCEECPublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } }
public void generateClientKeyExchange(OutputStream output) throws IOException { if (agreementCredentials != null) { return; } AsymmetricCipherKeyPair ecAgreeClientKeyPair = TlsECCUtils.generateECKeyPair(context.getSecureRandom(), ecAgreeServerPublicKey.getParameters()); this.ecAgreeClientPrivateKey = (ECPrivateKeyParameters)ecAgreeClientKeyPair.getPrivate(); byte[] point = TlsECCUtils.serializeECPublicKey(serverECPointFormats, (ECPublicKeyParameters)ecAgreeClientKeyPair.getPublic()); TlsUtils.writeOpaque8(point, output); }
/** * initialise the EC Elgamal engine. * * @param param the necessary EC key parameters. */ public void init( CipherParameters param) { if (param instanceof ParametersWithRandom) { ParametersWithRandom p = (ParametersWithRandom)param; if (!(p.getParameters() instanceof ECPublicKeyParameters)) { throw new IllegalArgumentException("ECPublicKeyParameters are required for new public key transform."); } this.key = (ECPublicKeyParameters)p.getParameters(); this.random = p.getRandom(); } else { if (!(param instanceof ECPublicKeyParameters)) { throw new IllegalArgumentException("ECPublicKeyParameters are required for new public key transform."); } this.key = (ECPublicKeyParameters)param; this.random = new SecureRandom(); } }
/** * initialise the encryptor. * * @param param the necessary EC key parameters. */ public void init( CipherParameters param) { if (param instanceof ParametersWithRandom) { ParametersWithRandom p = (ParametersWithRandom)param; if (!(p.getParameters() instanceof ECPublicKeyParameters)) { throw new IllegalArgumentException("ECPublicKeyParameters are required for encryption."); } this.key = (ECPublicKeyParameters)p.getParameters(); this.random = p.getRandom(); } else { if (!(param instanceof ECPublicKeyParameters)) { throw new IllegalArgumentException("ECPublicKeyParameters are required for encryption."); } this.key = (ECPublicKeyParameters)param; this.random = new SecureRandom(); } }
public void init(boolean forSigning, CipherParameters param) { if (forSigning) { if (param instanceof ParametersWithRandom) { ParametersWithRandom rParam = (ParametersWithRandom)param; this.random = rParam.getRandom(); param = rParam.getParameters(); } else { this.random = new SecureRandom(); } this.key = (ECPrivateKeyParameters)param; } else { this.key = (ECPublicKeyParameters)param; } }
public void init( boolean forSigning, CipherParameters param) { if (forSigning) { if (param instanceof ParametersWithRandom) { ParametersWithRandom rParam = (ParametersWithRandom)param; this.random = rParam.getRandom(); this.key = (ECPrivateKeyParameters)rParam.getParameters(); } else { this.random = new SecureRandom(); this.key = (ECPrivateKeyParameters)param; } } else { this.key = (ECPublicKeyParameters)param; } }
public void init( boolean forSigning, CipherParameters param) { this.forSigning = forSigning; if (forSigning) { if (param instanceof ParametersWithRandom) { ParametersWithRandom rParam = (ParametersWithRandom)param; this.random = rParam.getRandom(); this.key = (ECPrivateKeyParameters)rParam.getParameters(); } else { this.random = new SecureRandom(); this.key = (ECPrivateKeyParameters)param; } } else { this.key = (ECPublicKeyParameters)param; } }
public BCDSTU4145PublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } }
public BCECPublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec, ProviderConfiguration configuration) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } this.configuration = configuration; }
public BCECGOST3410PublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } }
JCEECPublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { this.ecSpec = new ECParameterSpec( dp.getCurve(), dp.getG(), dp.getN(), dp.getH(), dp.getSeed()); } else { this.ecSpec = spec; } }
/** * Given the domain parameters this routine generates an EC key * pair in accordance with X9.62 section 5.2.1 pages 26, 27. */ public AsymmetricCipherKeyPair generateKeyPair() { BigInteger n = params.getN(); int nBitLength = n.bitLength(); BigInteger d; do { d = new BigInteger(nBitLength, random); } while (d.equals(ZERO) || (d.compareTo(n) >= 0)); ECPoint Q = params.getG().multiply(d); return new AsymmetricCipherKeyPair( new ECPublicKeyParameters(Q, params), new ECPrivateKeyParameters(d, params)); }
@Override public void prepareAfterParse() { try { msg.prepareComputations(); List<ECPointFormat> pointFormatList = chooser.getServerSupportedPointFormats(); ECPointFormat[] formatArray = pointFormatList.toArray(new ECPointFormat[pointFormatList.size()]); short[] pointFormats = ECCUtilsBCWrapper.convertPointFormats(formatArray); ECPublicKeyParameters clientPublicKey = TlsECCUtils.deserializeECPublicKey(pointFormats, getDomainParameters(chooser.getEcCurveType(), chooser.getSelectedCurve()), msg.getPublicKey() .getValue()); CustomECPoint customClientKey = new CustomECPoint(clientPublicKey.getQ().getRawXCoord().toBigInteger(), clientPublicKey.getQ().getRawYCoord().toBigInteger()); msg.getComputations().setClientPublicKey(customClientKey); BigInteger privatekey = chooser.getServerEcPrivateKey(); computePremasterSecret(clientPublicKey, new ECPrivateKeyParameters(privatekey, clientPublicKey.getParameters())); preparePremasterSecret(msg); prepareClientRandom(msg); } catch (IOException ex) { throw new PreparationException("Could prepare ECDHClientKeyExchange Message after Parse", ex); } }
protected void adjustECParameter(ECDHEServerKeyExchangeMessage message) { tlsContext.setSelectedCurve(NamedCurve.getNamedCurve(message.getNamedCurve().getValue())); // TODO avoid BC tool byte[] ecParams = ArrayConverter.concatenate(new byte[] { message.getCurveType().getValue() }, message .getNamedCurve().getValue(), ArrayConverter.intToBytes(message.getPublicKeyLength().getValue(), 1), message.getPublicKey().getValue()); InputStream is = new ByteArrayInputStream(ecParams); ECPublicKeyParameters publicKeyParameters = null; try { publicKeyParameters = ECCUtilsBCWrapper.readECParametersWithPublicKey(is); } catch (TlsFatalAlert alert) { throw new AdjustmentException("Problematic EC parameters, we dont support these yet", alert); } catch (IOException ex) { throw new AdjustmentException("EC public key parsing failed", ex); } CustomECPoint publicKey = new CustomECPoint(publicKeyParameters.getQ().getRawXCoord().toBigInteger(), publicKeyParameters.getQ().getRawYCoord().toBigInteger()); tlsContext.setServerEcPublicKey(publicKey); }
public BigInteger calculateAgreement( CipherParameters pubKey) { ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey; ECDomainParameters params = pub.getParameters(); BigInteger hd = params.getH().multiply(key.getD()).mod(params.getN()); ECPoint P = pub.getQ().multiply(hd).normalize(); if (P.isInfinity()) { throw new IllegalStateException("Infinity is not a valid agreement value for ECDHC"); } return P.getAffineXCoord().toBigInteger(); }
public ECPoint calculatePoint(CipherParameters pubKey) { ECPublicKeyParameters pub = (ECPublicKeyParameters) pubKey; ECPoint P = pub.getQ().multiply(key.getD()).normalize(); if (P.isInfinity()) { throw new IllegalStateException("Infinity is not a valid agreement value for ECDH"); } return P; }
public JCEECPublicKey( String algorithm, ECPublicKeyParameters params) { this.algorithm = algorithm; this.q = params.getQ(); this.ecSpec = null; }
public static ECPublicKeyParameters deserializeECPublicKey(short[] ecPointFormats, ECDomainParameters curve_params, byte[] encoding) throws IOException { try { ECPoint Y = deserializeECPoint(ecPointFormats, curve_params.getCurve(), encoding); return new ECPublicKeyParameters(Y, curve_params); } catch (RuntimeException e) { throw new TlsFatalAlert(AlertDescription.illegal_parameter); } }
public static byte[] calculateECDHBasicAgreement(ECPublicKeyParameters publicKey, ECPrivateKeyParameters privateKey) { ECDHBasicAgreement basicAgreement = new ECDHBasicAgreement(); basicAgreement.init(privateKey); BigInteger agreementValue = basicAgreement.calculateAgreement(publicKey); /* * RFC 4492 5.10. Note that this octet string (Z in IEEE 1363 terminology) as output by * FE2OSP, the Field Element to Octet String Conversion Primitive, has constant length for * any given field; leading zeros found in this octet string MUST NOT be truncated. */ return BigIntegers.asUnsignedByteArray(basicAgreement.getFieldSize(), agreementValue); }
public BigInteger calculateAgreement( CipherParameters pubKey) { ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey; ECPoint P = pub.getQ().multiply(key.getD()); // if (p.isInfinity()) throw new RuntimeException("d*Q == infinity"); return P.getX().toBigInteger(); }
public BigInteger calculateAgreement( CipherParameters pubKey) { ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey; ECDomainParameters params = pub.getParameters(); ECPoint P = pub.getQ().multiply(params.getH().multiply(key.getD())); // if (p.isInfinity()) throw new RuntimeException("Invalid public key"); return P.getX().toBigInteger(); }