Java Code Examples for java.security.spec.ECPoint#getAffineX()
The following examples show how to use
java.security.spec.ECPoint#getAffineX() .
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: EcUtil.java From wycheproof with Apache License 2.0 | 6 votes |
/** * Checks that a point is on a given elliptic curve. This method implements the partial public key * validation routine from Section 5.6.2.6 of NIST SP 800-56A * http://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf A partial * public key validation is sufficient for curves with cofactor 1. See Section B.3 of * http://www.nsa.gov/ia/_files/SuiteB_Implementer_G-113808.pdf The point validations above are * taken from recommendations for ECDH, because parameter checks in ECDH are much more important * than for the case of ECDSA. Performing this test for ECDSA keys is mainly a sanity check. * * @param point the point that needs verification * @param ec the elliptic curve. This must be a curve over a prime order field. * @throws GeneralSecurityException if the field is binary or if the point is not on the curve. */ public static void checkPointOnCurve(ECPoint point, EllipticCurve ec) throws GeneralSecurityException { BigInteger p = getModulus(ec); BigInteger x = point.getAffineX(); BigInteger y = point.getAffineY(); if (x == null || y == null) { throw new GeneralSecurityException("point is at infinity"); } // Check 0 <= x < p and 0 <= y < p. if (x.signum() == -1 || x.compareTo(p) != -1) { throw new GeneralSecurityException("x is out of range"); } if (y.signum() == -1 || y.compareTo(p) != -1) { throw new GeneralSecurityException("y is out of range"); } // Check y^2 == x^3 + a x + b (mod p) BigInteger lhs = y.multiply(y).mod(p); BigInteger rhs = x.multiply(x).add(ec.getA()).multiply(x).add(ec.getB()).mod(p); if (!lhs.equals(rhs)) { throw new GeneralSecurityException("Point is not on curve"); } }
Example 2
Source File: Ssh2EcdsaSha2NistPublicKey.java From j2ssh-maverick with GNU Lesser General Public License v3.0 | 6 votes |
public static void main(String[] args) throws Exception { KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC"); ECGenParameterSpec gps = new ECGenParameterSpec ("secp256r1"); // NIST P-256 kpg.initialize(gps); KeyPair apair = kpg.generateKeyPair(); ECPublicKey apub = (ECPublicKey)apair.getPublic(); ECParameterSpec aspec = apub.getParams(); // could serialize aspec for later use (in compatible JRE) // // for test only reuse bogus pubkey, for real substitute values ECPoint apoint = apub.getW(); BigInteger x = apoint.getAffineX(), y = apoint.getAffineY(); // construct point plus params to pubkey ECPoint bpoint = new ECPoint (x,y); ECPublicKeySpec bpubs = new ECPublicKeySpec (bpoint, aspec); KeyFactory kfa = KeyFactory.getInstance ("EC"); ECPublicKey bpub = (ECPublicKey) kfa.generatePublic(bpubs); new Ssh2EcdsaSha2NistPublicKey(bpub); }
Example 3
Source File: EciesEncryption.java From protect with MIT License | 5 votes |
protected static byte[] encrypt(final byte[] message, final BigInteger r, final PublicKey publicKey) { if (publicKey instanceof ECPublicKey) { final ECPublicKey ecPublicKey = (ECPublicKey) publicKey; final ECPoint javaPoint = ecPublicKey.getW(); final EcPoint point = new EcPoint(javaPoint.getAffineX(), javaPoint.getAffineY()); return encrypt(message, r, point); } else { throw new IllegalArgumentException("Key type must be ECPublicKey!"); } }
Example 4
Source File: EciesEncryption.java From protect with MIT License | 5 votes |
protected static byte[] decrypt(final byte[] ciphertext, final BigInteger r, PublicKey publicKey) throws BadPaddingException, IllegalBlockSizeException { if (publicKey instanceof ECPublicKey) { final ECPublicKey ecPublicKey = (ECPublicKey) publicKey; final ECPoint javaPoint = ecPublicKey.getW(); final EcPoint point = new EcPoint(javaPoint.getAffineX(), javaPoint.getAffineY()); return decrypt(ciphertext, r, point); } else { throw new IllegalArgumentException("Key type must be ECPublicKey!"); } }
Example 5
Source File: EcUtil.java From wycheproof with Apache License 2.0 | 5 votes |
/** * Returns a weak public key of order 3 such that the public key point is on the curve specified * in ecParams. This method is used to check ECC implementations for missing step in the * verification of the public key. E.g. implementations of ECDH must verify that the public key * contains a point on the curve as well as public and secret key are using the same curve. * * @param ecParams the parameters of the key to attack. This must be a curve in Weierstrass form * over a prime order field. * @return a weak EC group with a genrator of order 3. */ public static ECPublicKeySpec getWeakPublicKey(ECParameterSpec ecParams) throws GeneralSecurityException { EllipticCurve curve = ecParams.getCurve(); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); keyGen.initialize(ecParams); BigInteger p = getModulus(curve); BigInteger three = new BigInteger("3"); while (true) { // Generate a point on the original curve KeyPair keyPair = keyGen.generateKeyPair(); ECPublicKey pub = (ECPublicKey) keyPair.getPublic(); ECPoint w = pub.getW(); BigInteger x = w.getAffineX(); BigInteger y = w.getAffineY(); // Find the curve parameters a,b such that 3*w = infinity. // This is the case if the following equations are satisfied: // 3x == l^2 (mod p) // l == (3x^2 + a) / 2*y (mod p) // y^2 == x^3 + ax + b (mod p) BigInteger l; try { l = modSqrt(x.multiply(three), p); } catch (GeneralSecurityException ex) { continue; } BigInteger xSqr = x.multiply(x).mod(p); BigInteger a = l.multiply(y.add(y)).subtract(xSqr.multiply(three)).mod(p); BigInteger b = y.multiply(y).subtract(x.multiply(xSqr.add(a))).mod(p); EllipticCurve newCurve = new EllipticCurve(curve.getField(), a, b); // Just a sanity check. checkPointOnCurve(w, newCurve); // Cofactor and order are of course wrong. ECParameterSpec spec = new ECParameterSpec(newCurve, w, p, 1); return new ECPublicKeySpec(w, spec); } }
Example 6
Source File: EcPointDef.java From swim with Apache License 2.0 | 4 votes |
public static EcPointDef from(ECPoint point) { return new EcPointDef(point.getAffineX(), point.getAffineY()); }
Example 7
Source File: ECPointTest.java From j2objc with Apache License 2.0 | 3 votes |
/** * Test #1 for <code>getAffineX()</code> method<br> * Assertion: returns affine <code>x</code> coordinate<br> * Test preconditions: <code>ECPoint</code> instance * created using valid parameters<br> * Expected: must return affine <code>x</code> coordinate * which is equal to the one passed to the constructor; * (both must refer the same object) */ public final void testGetAffineX01() { BigInteger x = BigInteger.valueOf(-23456L); ECPoint p = new ECPoint(x, BigInteger.valueOf(23456L)); BigInteger xRet = p.getAffineX(); assertEquals(x, xRet); assertSame(x, xRet); }