org.spongycastle.jce.ECNamedCurveTable Java Examples

The following examples show how to use org.spongycastle.jce.ECNamedCurveTable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpenIdSigningKeyResolver.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
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;
    }
}
 
Example #2
Source File: ProvisioningPublicKeyState.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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();
        }
    }
 
Example #3
Source File: KeyCodec.java    From UAF with Apache License 2.0 5 votes vote down vote up
public static KeyPair generate() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    SecureRandom random = new SecureRandom();
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1");
    KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA");
    g.initialize(ecSpec, random);
    return g.generateKeyPair();
}
 
Example #4
Source File: KeyCodec.java    From UAF with Apache License 2.0 5 votes vote down vote up
/**
 * Decode based on X, Y 32 byte integers
 *
 * @param pubKey
 * @param curveName - Example secp256r1
 * @return
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
public static PublicKey getPubKeyFromCurve(byte[] pubKey, String curveName) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {

    ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName);
    KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider());
    ECNamedCurveSpec params = new ECNamedCurveSpec(curveName, 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;
}
 
Example #5
Source File: KeyCodec.java    From UAF with Apache License 2.0 5 votes vote down vote up
/**
 * Decode based on d - 32 byte integer
 *
 * @param privKey
 * @param curveName - Example secp256r1
 * @return
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
public static PrivateKey getPrivKeyFromCurve(byte[] privKey, String curveName) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {

    ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName);
    KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider());
    ECNamedCurveSpec params = new ECNamedCurveSpec(curveName, spec.getCurve(), spec.getG(), spec.getN());
    ECPrivateKeySpec priKey = new ECPrivateKeySpec(
            new BigInteger(privKey), // d
            params);
    return kf.generatePrivate(priKey);
}