Java Code Examples for java.security.interfaces.ECPrivateKey#getS()

The following examples show how to use java.security.interfaces.ECPrivateKey#getS() . 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: EcKeyTest.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
/**
 * Tests key generation for given parameters. The test can be skipped if the curve is not a
 * standard curve.
 */
void testKeyGeneration(ECParameterSpec ecParams, boolean isStandard) throws Exception {
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
  KeyPair keyPair;
  try {
    keyGen.initialize(ecParams);
    keyPair = keyGen.generateKeyPair();
  } catch (InvalidAlgorithmParameterException ex) {
    if (!isStandard) {
      return;
    }
    throw ex;
  }
  ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
  ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
  EcUtil.checkPublicKey(pub);
  BigInteger s = priv.getS();
  // Check the length of s. Could fail with probability 2^{-32}.
  assertTrue(s.bitLength() >= EcUtil.fieldSizeInBits(ecParams.getCurve()) - 32);
  // TODO(bleichen): correct curve?
  // TODO(bleichen): use RandomUtil
}
 
Example 2
Source File: EcdsaTest.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
/** Extract the k that was used to sign the signature. */
BigInteger extractK(byte[] signature, BigInteger h, ECPrivateKey priv) throws Exception {
  BigInteger x = priv.getS();
  BigInteger n = priv.getParams().getOrder();
  BigInteger r = extractR(signature);
  BigInteger s = extractS(signature);
  BigInteger k = x.multiply(r).add(h).multiply(s.modInverse(n)).mod(n);
  return k;
}
 
Example 3
Source File: EciesEncryption.java    From protect with MIT License 5 votes vote down vote up
protected static byte[] decrypt(final byte[] ciphertext, final PrivateKey privateKey)
		throws BadPaddingException, IllegalBlockSizeException {
	if (privateKey instanceof ECPrivateKey) {
		final ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey;
		final BigInteger privateKeyInt = ecPrivateKey.getS();
		return decrypt(ciphertext, privateKeyInt);
	} else {
		throw new IllegalArgumentException("Key type must be ECPublicKey!");
	}
}
 
Example 4
Source File: JCEECPrivateKey.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public JCEECPrivateKey(
    ECPrivateKey    key)
{
    this.d = key.getS();
    this.algorithm = key.getAlgorithm();
    this.ecSpec = key.getParams();
}
 
Example 5
Source File: BCDSTU4145PrivateKey.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public BCDSTU4145PrivateKey(
    ECPrivateKey key)
{
    this.d = key.getS();
    this.algorithm = key.getAlgorithm();
    this.ecSpec = key.getParams();
}
 
Example 6
Source File: BCECPrivateKey.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public BCECPrivateKey(
    ECPrivateKey key,
    ProviderConfiguration configuration)
{
    this.d = key.getS();
    this.algorithm = key.getAlgorithm();
    this.ecSpec = key.getParams();
    this.configuration = configuration;
}
 
Example 7
Source File: BCECGOST3410PrivateKey.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public BCECGOST3410PrivateKey(
    ECPrivateKey key)
{
    this.d = key.getS();
    this.algorithm = key.getAlgorithm();
    this.ecSpec = key.getParams();
}
 
Example 8
Source File: JCEECPrivateKey.java    From ripple-lib-java with ISC License 5 votes vote down vote up
public JCEECPrivateKey(
    ECPrivateKey    key)
{
    this.d = key.getS();
    this.algorithm = key.getAlgorithm();
    this.ecSpec = key.getParams();
}
 
Example 9
Source File: BCDSTU4145PrivateKey.java    From ripple-lib-java with ISC License 5 votes vote down vote up
public BCDSTU4145PrivateKey(
    ECPrivateKey key)
{
    this.d = key.getS();
    this.algorithm = key.getAlgorithm();
    this.ecSpec = key.getParams();
}
 
Example 10
Source File: BCECPrivateKey.java    From ripple-lib-java with ISC License 5 votes vote down vote up
public BCECPrivateKey(
    ECPrivateKey key,
    ProviderConfiguration configuration)
{
    this.d = key.getS();
    this.algorithm = key.getAlgorithm();
    this.ecSpec = key.getParams();
    this.configuration = configuration;
}
 
Example 11
Source File: BCECGOST3410PrivateKey.java    From ripple-lib-java with ISC License 5 votes vote down vote up
public BCECGOST3410PrivateKey(
    ECPrivateKey key)
{
    this.d = key.getS();
    this.algorithm = key.getAlgorithm();
    this.ecSpec = key.getParams();
}
 
Example 12
Source File: EcPrivateKeyDef.java    From swim with Apache License 2.0 4 votes vote down vote up
public static EcPrivateKeyDef from(ECPrivateKey key) {
  return new EcPrivateKeyDef(EcDomainDef.from(key.getParams()), key.getS(), key);
}
 
Example 13
Source File: CKey.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
static byte[] generateECBlob(Key k) {

        int keyBitLength = KeyUtil.getKeySize(k);
        int keyLen = (keyBitLength + 7) / 8;
        boolean isPrivate = k instanceof ECPrivateKey;

        byte[] keyBlob = new byte[8 + keyLen * (isPrivate ? 3 : 2)];
        keyBlob[0] = 'E';
        keyBlob[1] = 'C';
        keyBlob[2] = 'S';
        if (isPrivate) {
            keyBlob[3] = (byte) (keyBitLength == 256 ? '2'
                    : (keyBitLength == 384 ? '4' : '6'));
        } else {
            keyBlob[3] = (byte) (keyBitLength == 256 ? '1'
                    : (keyBitLength == 384 ? '3' : '5'));
        }
        BigInteger x;
        BigInteger y;
        // Fill the array in reverse order (s -> y -> x -> len) in case
        // one BigInteger encoding has an extra 0 at the beginning
        if (isPrivate) {
            // We can keep X and Y zero and it still works
            ECPrivateKey prk = (ECPrivateKey)k;
            BigInteger s = prk.getS();
            byte[] bs = s.toByteArray();
            System.arraycopy(
                    bs, 0,
                    keyBlob, 8 + keyLen + keyLen + keyLen - bs.length,
                    bs.length);
        } else {
            ECPublicKey puk = (ECPublicKey)k;
            x = puk.getW().getAffineX();
            y = puk.getW().getAffineY();
            byte[] by = y.toByteArray();
            System.arraycopy(by, 0, keyBlob, 8 + keyLen + keyLen - by.length,
                    by.length);
            byte[] bx = x.toByteArray();
            System.arraycopy(bx, 0, keyBlob, 8 + keyLen - bx.length, bx.length);
        }
        keyBlob[4] = (byte) keyLen;
        keyBlob[5] = keyBlob[6] = keyBlob[7] = 0;
        return keyBlob;
    }
 
Example 14
Source File: SecurityUtils.java    From RISE-V2G with MIT License 4 votes vote down vote up
/**
 * Checks if the private key is a valid key (according to requirement [V2G2-823]) for the received contract 
 * certificate before saving it to the keystore.
 * @param privateKey The private key corresponding to the contract certificate
 * @param contractCertChain The received contract certificate chain 
 * @return True, if the private key is a valid key, false otherwise.
 */
private static boolean isPrivateKeyValid(ECPrivateKey privateKey, CertificateChainType contractCertChain) {
	AlgorithmParameters parameters;
	
	try {
		parameters = AlgorithmParameters.getInstance("EC");
		parameters.init(new ECGenParameterSpec("secp256r1"));
		
		ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);
		
		// Now we need to check if the private key is correct (see requirement [V2G2-823]) 
		BigInteger order = ecParameterSpec.getOrder();
		ECPoint basePoint = ecParameterSpec.getGenerator();
		BigInteger privateKeyValue = privateKey.getS();
		X509Certificate contractCert = getCertificate(contractCertChain.getCertificate());
		ECPublicKey publicKey = (ECPublicKey) contractCert.getPublicKey();
		
		// 1. check
		if (privateKeyValue.compareTo(order) != -1) {
			getLogger().error("Validation of private key failed: its value is not strictly smaller than the "
							+ "order of the base point");
			return false;
		}
		
		// 2. check
		/*
		 * TODO: 
		 * No idea how to check for 
		 * "multiplication of the base point with this value must generate a key matching the public key of 
		 * the contract certificate"
		 * "this value" = value of private key
		 * -> some more expert knowledge on the arithmetic of elliptic curves is needed to tackle this!
		 */
		
	} catch (NoSuchAlgorithmException | InvalidParameterSpecException e) {
		getLogger().error(e.getClass().getSimpleName() + " occurred when trying to get private key from raw bytes", e);
		return false;
	}
	
	return true;
}
 
Example 15
Source File: CKey.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
static byte[] generateECBlob(Key k) {

        int keyBitLength = KeyUtil.getKeySize(k);
        int keyLen = (keyBitLength + 7) / 8;
        boolean isPrivate = k instanceof ECPrivateKey;

        byte[] keyBlob = new byte[8 + keyLen * (isPrivate ? 3 : 2)];
        keyBlob[0] = 'E';
        keyBlob[1] = 'C';
        keyBlob[2] = 'S';
        if (isPrivate) {
            keyBlob[3] = (byte) (keyBitLength == 256 ? '2'
                    : (keyBitLength == 384 ? '4' : '6'));
        } else {
            keyBlob[3] = (byte) (keyBitLength == 256 ? '1'
                    : (keyBitLength == 384 ? '3' : '5'));
        }
        BigInteger x;
        BigInteger y;
        // Fill the array in reverse order (s -> y -> x -> len) in case
        // one BigInteger encoding has an extra 0 at the beginning
        if (isPrivate) {
            // We can keep X and Y zero and it still works
            ECPrivateKey prk = (ECPrivateKey)k;
            BigInteger s = prk.getS();
            byte[] bs = s.toByteArray();
            System.arraycopy(
                    bs, 0,
                    keyBlob, 8 + keyLen + keyLen + keyLen - bs.length,
                    bs.length);
        } else {
            ECPublicKey puk = (ECPublicKey)k;
            x = puk.getW().getAffineX();
            y = puk.getW().getAffineY();
            byte[] by = y.toByteArray();
            System.arraycopy(by, 0, keyBlob, 8 + keyLen + keyLen - by.length,
                    by.length);
            byte[] bx = x.toByteArray();
            System.arraycopy(bx, 0, keyBlob, 8 + keyLen - bx.length, bx.length);
        }
        keyBlob[4] = (byte) keyLen;
        keyBlob[5] = keyBlob[6] = keyBlob[7] = 0;
        return keyBlob;
    }