/** * Convert a byte array to an EC private key by decoding the D number * parameter. * * @param keyBytes Bytes to be converted to the EC private key. * @return An instance of EC private key decoded from the input bytes. * @throws InvalidKeySpecException The provided key bytes are not a valid EC * private key. * @throws CryptoProviderException When crypto provider is incorrectly initialized. */ @Override public PrivateKey convertBytesToPrivateKey(byte[] keyBytes) throws InvalidKeySpecException, CryptoProviderException { try { KeyFactory kf = KeyFactory.getInstance("ECDH", getProviderName()); BigInteger keyInteger = new BigInteger(keyBytes); ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); ECPrivateKeySpec pubSpec = new ECPrivateKeySpec(keyInteger, ecSpec); return kf.generatePrivate(pubSpec); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new CryptoProviderException(ex.getMessage(), ex); } }
/** * return an enumeration of the names of the available curves. * * @return an enumeration of the names of the available curves. */ public static Enumeration getNames() { Vector v = new Vector(); addEnumeration(v, X962NamedCurves.getNames()); addEnumeration(v, SECNamedCurves.getNames()); addEnumeration(v, NISTNamedCurves.getNames()); addEnumeration(v, TeleTrusTNamedCurves.getNames()); return v.elements(); }
/** * Convert a byte array to an EC private key by decoding the D number * parameter. * * @param keyBytes Bytes to be converted to the EC private key. * @return An instance of EC private key decoded from the input bytes. * @throws InvalidKeySpecException The provided key bytes are not a valid EC * private key. * @throws CryptoProviderException When crypto provider is incorrectly initialized. */ @Override public PrivateKey convertBytesToPrivateKey(byte[] keyBytes) throws InvalidKeySpecException, CryptoProviderException { try { KeyFactory kf = KeyFactory.getInstance("ECDH", getProviderName()); BigInteger keyInteger = new BigInteger(keyBytes); ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); ECPrivateKeySpec pubSpec = new ECPrivateKeySpec(keyInteger, ecSpec); return kf.generatePrivate(pubSpec); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new CryptoProviderException(ex.getMessage(), ex); } }
private void generateKeyPairs() { try { final ECNamedCurveParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDH", "SC"); keyPairGenerator.initialize(parameterSpec); final KeyPair keyPair = keyPairGenerator.generateKeyPair(); final ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic(); mProvisionerPrivaetKey = (ECPrivateKey) keyPair.getPrivate(); final ECPoint point = publicKey.getQ(); final BigInteger x = point.getXCoord().toBigInteger(); final BigInteger y = point.getYCoord().toBigInteger(); final byte[] tempX = BigIntegers.asUnsignedByteArray(32, x); final byte[] tempY = BigIntegers.asUnsignedByteArray(32, y); Log.v(TAG, "X: length: " + tempX.length + " " + MeshParserUtils.bytesToHex(tempX, false)); Log.v(TAG, "Y: length: " + tempY.length + " " + MeshParserUtils.bytesToHex(tempY, false)); final byte[] tempXY = new byte[64]; System.arraycopy(tempX, 0, tempXY, 0, tempX.length); System.arraycopy(tempY, 0, tempXY, tempY.length, tempY.length); mUnprovisionedMeshNode.setProvisionerPublicKeyXY(tempXY); Log.v(TAG, "XY: " + MeshParserUtils.bytesToHex(tempXY, true)); } catch (Exception e) { e.printStackTrace(); } }
/** * Converts byte array to an EC public key, by decoding the Q point * parameter. * * @param keyBytes Bytes to be converted to EC public key. * @return An instance of the EC public key on success, or null on failure. * @throws InvalidKeySpecException When provided bytes are not a correct key * representation. * @throws CryptoProviderException When crypto provider is incorrectly initialized. */ @Override public PublicKey convertBytesToPublicKey(byte[] keyBytes) throws InvalidKeySpecException, CryptoProviderException { try { KeyFactory kf = KeyFactory.getInstance("ECDH", getProviderName()); ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); if (ecSpec == null) { // can happen with incorrectly initialized crypto provider throw new CryptoProviderException("Crypto provider does not support the secp256r1 curve"); } ECPoint point = ecSpec.getCurve().decodePoint(keyBytes); ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, ecSpec); return kf.generatePublic(pubSpec); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new CryptoProviderException(ex.getMessage(), ex); } }
/** * Converts byte array to an EC public key, by decoding the Q point * parameter. * * @param keyBytes Bytes to be converted to EC public key. * @return An instance of the EC public key on success, or null on failure. * @throws InvalidKeySpecException When provided bytes are not a correct key * representation. * @throws CryptoProviderException When crypto provider is incorrectly initialized. */ @Override public PublicKey convertBytesToPublicKey(byte[] keyBytes) throws InvalidKeySpecException, CryptoProviderException { try { KeyFactory kf = KeyFactory.getInstance("ECDH", getProviderName()); ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); if (ecSpec == null) { // can happen with incorrectly initialized crypto provider throw new CryptoProviderException("Crypto provider does not support the secp256r1 curve"); } ECPoint point = ecSpec.getCurve().decodePoint(keyBytes); ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, ecSpec); return kf.generatePublic(pubSpec); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new CryptoProviderException(ex.getMessage(), ex); } }
private static Key loadEcPublicKey(final byte [] pubKey, final EcCurve curveName) throws NoSuchAlgorithmException, InvalidKeySpecException { final ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName.toString()); KeyFactory kf; try { kf = KeyFactory.getInstance(ECDH, BouncyCastleProvider.PROVIDER_NAME); } catch (final NoSuchProviderException e) { LOGGER.warning( "No se ha podido obtener el KeyFactory ECDH de BouncyCastle, se intentara el por defecto: " + e //$NON-NLS-1$ ); kf = KeyFactory.getInstance(ECDH); } final ECNamedCurveSpec params = new ECNamedCurveSpec(curveName.toString(), spec.getCurve(), spec.getG(), spec.getN()); final ECPoint point = ECPointUtil.decodePoint(params.getCurve(), pubKey); final java.security.spec.ECPublicKeySpec pubKeySpec = new java.security.spec.ECPublicKeySpec(point, params); return kf.generatePublic(pubKeySpec); }
/** * <p>Get an EC public key from a byte array suitable for use with ssh operations</p> * * <p>Note: This is not a Bitcoin EC public key</p> * * @param pubKey The ecdsa-sha2-nistp256 EC public key encoded as bytes * * @return An EC public key * * @throws NoSuchAlgorithmException If ECDSA is not available * @throws InvalidKeySpecException If the key is invalid */ public static ECPublicKey getPublicKeyFromBytes(byte[] pubKey) throws NoSuchAlgorithmException, InvalidKeySpecException { ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("P-256"); KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider()); ECNamedCurveSpec params = new ECNamedCurveSpec("P-256", spec.getCurve(), spec.getG(), spec.getN()); ECPoint point = ECPointUtil.decodePoint(params.getCurve(), pubKey); ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params); ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec); return pk; }
@Override public AlgorithmParameterSpec getEcPoint(final byte[] nonceS, final byte[] sharedSecretH, final EcCurve curveName) { final AlgorithmParameterSpec ecParams = ECNamedCurveTable.getParameterSpec(curveName.toString()); final BigInteger affineX = os2i(sharedSecretH); final BigInteger affineY = computeAffineY(affineX, (ECParameterSpec) ecParams); final ECPoint sharedSecretPointH = new ECPoint(affineX, affineY); return mapNonceGMWithECDH(os2i(nonceS), sharedSecretPointH, (ECParameterSpec) ecParams); }
final BigInteger y = BigIntegers.fromUnsignedByteArray(xy, 32, 32); final ECParameterSpec ecParameters = ECNamedCurveTable.getParameterSpec("secp256r1"); ECCurve curve = ecParameters.getCurve(); ECPoint ecPoint = curve.validatePoint(x, y);
private static ECPublicKey generateECPublicKey(final JWK jwk) { final BigInteger x = decodeBase64(jwk.getX()); final BigInteger y = decodeBase64(jwk.getY()); try { final KeyFactory factory = KeyFactory.getInstance("EC"); final ECPoint point = new ECPoint(x, y); final ECNamedCurveParameterSpec paramSpec = ECNamedCurveTable.getParameterSpec(jwk.getCurve()); final ECNamedCurveSpec params = new ECNamedCurveSpec(jwk.getCurve(), paramSpec.getCurve(), paramSpec.getG(), paramSpec.getN()); final ECPublicKeySpec spec = new ECPublicKeySpec(point, params); return (ECPublicKey) factory.generatePublic(spec); } catch (final NoSuchAlgorithmException | InvalidKeySpecException e) { Log.e(TAG, "failed to generate EC Public Key from JWK: " + jwk, e); return null; } }
/** * <p> * Get an EC public key from a byte array suitable for use with ssh * operations</p> * * <p> * Note: This is not a Bitcoin EC public key</p> * * @param pubKey The ecdsa-sha2-nistp256 EC public key encoded as bytes * * @return An EC public key * * @throws NoSuchAlgorithmException If ECDSA is not available * @throws InvalidKeySpecException If the key is invalid */ public static ECPublicKey decodeNISTP256PublicKeyFromBytes(byte[] pubKey) throws NoSuchAlgorithmException, InvalidKeySpecException { ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("P-256"); KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider()); ECNamedCurveSpec params = new ECNamedCurveSpec("P-256", spec.getCurve(), spec.getG(), spec.getN()); ECPoint point = ECPointUtil.decodePoint(params.getCurve(), pubKey); ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params); ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec); return pk; }
public CryptoImpl() { // populate the cipher set map CipherSet2aImpl set2a = new CipherSet2aImpl(this); mCipherSetMap.put(set2a.getCipherSetId(), set2a); // initialize elliptic curve parameters and generator mECNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec("prime256v1"); mECGenerator = new ECKeyPairGenerator(); mECDomainParameters = new ECDomainParameters( mECNamedCurveParameterSpec.getCurve(), mECNamedCurveParameterSpec.getG(), mECNamedCurveParameterSpec.getN() ); mECKeyGenerationParameters = new ECKeyGenerationParameters(mECDomainParameters, random); mECGenerator.init(mECKeyGenerationParameters); }
public CipherSet2aImpl(Crypto crypto) { mCrypto = crypto; // initialize elliptic curve parameters and generator mECNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec("prime256v1"); mECGenerator = new ECKeyPairGenerator(); mECDomainParameters = new ECDomainParameters( mECNamedCurveParameterSpec.getCurve(), mECNamedCurveParameterSpec.getG(), mECNamedCurveParameterSpec.getN() ); mECKeyGenerationParameters = new ECKeyGenerationParameters(mECDomainParameters, mRandom); mECGenerator.init(mECKeyGenerationParameters); }