use of java.security.spec.ECPrivateKeySpec in project wycheproof by google.
the class JsonEcdhTest method testEcdhComp.
/**
* Example for test vector
* {
* "algorithm" : "ECDH",
* "header" : [],
* "notes" : {
* "AddSubChain" : "The private key has a special value....",
* }
* "generatorVersion" : "0.7",
* "numberOfTests" : 308,
* "testGroups" : [
* {
* "type" : "EcdhTest",
* "tests" : [
* {
* "comment" : "normal case",
* "curve" : "secp224r1",
* "private" : "565577a49415ca761a0322ad54e4ad0ae7625174baf372c2816f5328",
* "public" : "30...",
* "result" : "valid",
* "shared" : "b8ecdb552d39228ee332bafe4886dbff272f7109edf933bc7542bd4f",
* "tcId" : 1
* },
* ...
*/
public void testEcdhComp(String filename) throws Exception {
JsonObject test = JsonUtil.getTestVectors(filename);
// This test expects test vectors as defined in wycheproof/schemas/ecdh_test_schema.json.
// In particular, this means that the public keys use X509 encoding.
// Test vectors with different encodings of the keys have a different schema.
final String expectedSchema = "ecdh_test_schema.json";
String schema = test.get("schema").getAsString();
assertEquals("Unexpected schema in file:" + filename, expectedSchema, schema);
int numTests = test.get("numberOfTests").getAsInt();
int passedTests = 0;
// invalid test vectors leading to exceptions
int rejectedTests = 0;
// valid test vectors leading to exceptions
int skippedTests = 0;
int errors = 0;
for (JsonElement g : test.getAsJsonArray("testGroups")) {
JsonObject group = g.getAsJsonObject();
String curve = getString(group, "curve");
for (JsonElement t : group.getAsJsonArray("tests")) {
JsonObject testcase = t.getAsJsonObject();
int tcid = testcase.get("tcId").getAsInt();
String comment = getString(testcase, "comment");
BigInteger priv = getBigInteger(testcase, "private");
byte[] publicEncoded = getBytes(testcase, "public");
String result = getString(testcase, "result");
String expectedHex = getString(testcase, "shared");
KeyFactory kf = KeyFactory.getInstance("EC");
try {
ECPrivateKeySpec spec = new ECPrivateKeySpec(priv, EcUtil.getCurveSpecRef(curve));
PrivateKey privKey = kf.generatePrivate(spec);
X509EncodedKeySpec x509keySpec = new X509EncodedKeySpec(publicEncoded);
PublicKey pubKey = kf.generatePublic(x509keySpec);
KeyAgreement ka = KeyAgreement.getInstance("ECDH");
ka.init(privKey);
ka.doPhase(pubKey, true);
String sharedHex = TestUtil.bytesToHex(ka.generateSecret());
if (result.equals("invalid")) {
System.out.println("Computed ECDH with invalid parameters" + " tcId:" + tcid + " comment:" + comment + " shared:" + sharedHex);
errors++;
} else if (!expectedHex.equals(sharedHex)) {
System.out.println("Incorrect ECDH computation" + " tcId:" + tcid + " comment:" + comment + "\nshared:" + sharedHex + "\nexpected:" + expectedHex);
errors++;
} else {
passedTests++;
}
} catch (InvalidKeySpecException | InvalidKeyException | NoSuchAlgorithmException ex) {
// or when a key is not valid.
if (result.equals("valid")) {
skippedTests++;
} else {
rejectedTests++;
}
} catch (Exception ex) {
// Other exceptions typically indicate that something is wrong with the implementation.
System.out.println("Test vector with tcId:" + tcid + " comment:" + comment + " throws:" + ex.toString());
errors++;
}
}
}
assertEquals(0, errors);
assertEquals(numTests, passedTests + rejectedTests + skippedTests);
}
use of java.security.spec.ECPrivateKeySpec in project i2p.i2p-bote by i2p.
the class ECDH_ECDSA method createPrivateKeyPair.
@Override
public PrivateKeyPair createPrivateKeyPair(byte[] bytes) throws GeneralSecurityException {
PrivateKeyPair keyPair = new PrivateKeyPair();
ECPrivateKeySpec encryptionKeySpec = createPrivateKeySpec(Arrays.copyOf(bytes, keyLengthBytes));
keyPair.encryptionKey = ecdhKeyFactory.generatePrivate(encryptionKeySpec);
ECPrivateKeySpec signingKeySpec = createPrivateKeySpec(Arrays.copyOfRange(bytes, keyLengthBytes, 2 * keyLengthBytes));
keyPair.signingKey = ecdsaKeyFactory.generatePrivate(signingKeySpec);
return keyPair;
}
use of java.security.spec.ECPrivateKeySpec in project j2objc by google.
the class ECPrivateKeySpecTest method setUp.
protected void setUp() throws Exception {
super.setUp();
ECPoint ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
EllipticCurve curve = new EllipticCurve(new ECFieldF2m(2), BigInteger.valueOf(1), BigInteger.valueOf(1));
s = BigInteger.valueOf(1);
ecparams = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
ecpks = new ECPrivateKeySpec(s, ecparams);
}
use of java.security.spec.ECPrivateKeySpec in project robovm by robovm.
the class NativeCryptoTest method initChannelIdKey.
private static synchronized void initChannelIdKey() throws Exception {
if (CHANNEL_ID_PRIVATE_KEY != null) {
return;
}
// NIST P-256 aka SECG secp256r1 aka X9.62 prime256v1
OpenSSLECGroupContext openSslSpec = OpenSSLECGroupContext.getCurveByName("prime256v1");
BigInteger s = new BigInteger("229cdbbf489aea584828a261a23f9ff8b0f66f7ccac98bf2096ab3aee41497c5", 16);
CHANNEL_ID_PRIVATE_KEY = new OpenSSLECPrivateKey(new ECPrivateKeySpec(s, openSslSpec.getECParameterSpec())).getOpenSSLKey();
// Channel ID is the concatenation of the X and Y coordinates of the public key.
CHANNEL_ID = new BigInteger("702b07871fd7955c320b26f15e244e47eed60272124c92b9ebecf0b42f90069b" + "ab53592ebfeb4f167dbf3ce61513afb0e354c479b1c1b69874fa471293494f77", 16).toByteArray();
}
use of java.security.spec.ECPrivateKeySpec in project cxf by apache.
the class CryptoUtils method getECPrivateKey.
public static ECPrivateKey getECPrivateKey(String curve, byte[] privateKey) {
try {
ECParameterSpec params = getECParameterSpec(curve, true);
ECPrivateKeySpec keySpec = new ECPrivateKeySpec(toBigInteger(privateKey), params);
KeyFactory kf = KeyFactory.getInstance("EC");
return (ECPrivateKey) kf.generatePrivate(keySpec);
} catch (Exception ex) {
throw new SecurityException(ex);
}
}
Aggregations