use of java.security.interfaces.ECPrivateKey in project wycheproof by google.
the class EcdsaTest method testBasic.
/**
* This test checks the basic functionality of ECDSA. It can also be used to generate simple test
* vectors.
*/
public void testBasic() throws Exception {
String algorithm = "SHA256WithECDSA";
String hashAlgorithm = "SHA-256";
String message = "Hello";
String curve = "secp256r1";
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
keyGen.initialize(ecSpec);
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
byte[] messageBytes = message.getBytes("UTF-8");
Signature signer = Signature.getInstance(algorithm);
Signature verifier = Signature.getInstance(algorithm);
signer.initSign(priv);
signer.update(messageBytes);
byte[] signature = signer.sign();
verifier.initVerify(pub);
verifier.update(messageBytes);
assertTrue(verifier.verify(signature));
// Extract some parameters.
byte[] rawHash = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
ECParameterSpec params = priv.getParams();
// Print keys and signature, so that it can be used to generate new test vectors.
System.out.println("Message:" + message);
System.out.println("Hash:" + TestUtil.bytesToHex(rawHash));
System.out.println("Curve:" + curve);
System.out.println("Order:" + params.getOrder().toString());
System.out.println("Private key:");
System.out.println("S:" + priv.getS().toString());
System.out.println("encoded:" + TestUtil.bytesToHex(priv.getEncoded()));
System.out.println("Public key:");
ECPoint w = pub.getW();
System.out.println("X:" + w.getAffineX().toString());
System.out.println("Y:" + w.getAffineY().toString());
System.out.println("encoded:" + TestUtil.bytesToHex(pub.getEncoded()));
System.out.println("Signature:" + TestUtil.bytesToHex(signature));
System.out.println("r:" + extractR(signature).toString());
System.out.println("s:" + extractS(signature).toString());
}
use of java.security.interfaces.ECPrivateKey in project wycheproof by google.
the class EciesTest method testKeyGeneration.
/**
* BouncyCastle has a key generation algorithm "ECIES". This test checks that the result are
* ECKeys in both cases.
*/
public void testKeyGeneration() throws Exception {
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
KeyPairGenerator kf = KeyPairGenerator.getInstance("ECIES");
kf.initialize(ecSpec);
KeyPair keyPair = kf.generateKeyPair();
ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
}
use of java.security.interfaces.ECPrivateKey in project chromeview by pwnall.
the class AndroidKeyStore method rawSignDigestWithPrivateKey.
/**
* Sign a given message with a given PrivateKey object. This method
* shall only be used to implement signing in the context of SSL
* client certificate support.
*
* The message will actually be a hash, computed and padded by OpenSSL,
* itself, depending on the type of the key. The result should match
* exactly what the vanilla implementations of the following OpenSSL
* function calls do:
*
* - For a RSA private key, this should be equivalent to calling
* RSA_sign(NDI_md5_sha1,....), i.e. it must generate a raw RSA
* signature. The message must a combined, 36-byte MD5+SHA1 message
* digest padded to the length of the modulus using PKCS#1 padding.
*
* - For a DSA and ECDSA private keys, this should be equivalent to
* calling DSA_sign(0,...) and ECDSA_sign(0,...) respectively. The
* message must be a 20-byte SHA1 hash and the function shall
* compute a direct DSA/ECDSA signature for it.
*
* @param privateKey The PrivateKey handle.
* @param message The message to sign.
* @return signature as a byte buffer.
*
* Important: Due to a platform bug, this function will always fail on
* Android < 4.2 for RSA PrivateKey objects. See the
* getOpenSSLHandleForPrivateKey() below for work-around.
*/
@CalledByNative
public static byte[] rawSignDigestWithPrivateKey(PrivateKey privateKey, byte[] message) {
// Get the Signature for this key.
Signature signature = null;
// http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html
try {
if (privateKey instanceof RSAPrivateKey) {
// IMPORTANT: Due to a platform bug, this will throw NoSuchAlgorithmException
// on Android 4.0.x and 4.1.x. Fixed in 4.2 and higher.
// See https://android-review.googlesource.com/#/c/40352/
signature = Signature.getInstance("NONEwithRSA");
} else if (privateKey instanceof DSAPrivateKey) {
signature = Signature.getInstance("NONEwithDSA");
} else if (privateKey instanceof ECPrivateKey) {
signature = Signature.getInstance("NONEwithECDSA");
}
} catch (NoSuchAlgorithmException e) {
;
}
if (signature == null) {
Log.e(TAG, "Unsupported private key algorithm: " + privateKey.getAlgorithm());
return null;
}
// Sign the message.
try {
signature.initSign(privateKey);
signature.update(message);
return signature.sign();
} catch (Exception e) {
Log.e(TAG, "Exception while signing message with " + privateKey.getAlgorithm() + " private key: " + e);
return null;
}
}
use of java.security.interfaces.ECPrivateKey in project robovm by robovm.
the class OpenSSLECPrivateKey method equals.
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof OpenSSLECPrivateKey) {
OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o;
return key.equals(other.key);
}
if (!(o instanceof ECPrivateKey)) {
return false;
}
final ECPrivateKey other = (ECPrivateKey) o;
if (!getPrivateKey().equals(other.getS())) {
return false;
}
final ECParameterSpec spec = getParams();
final ECParameterSpec otherSpec = other.getParams();
return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor();
}
use of java.security.interfaces.ECPrivateKey in project robovm by robovm.
the class KeyPairGeneratorTest method test_KeyWithAllKeyFactories.
private void test_KeyWithAllKeyFactories(Key k) throws Exception {
byte[] encoded = k.getEncoded();
String keyAlgo = k.getAlgorithm();
Provider[] providers = Security.getProviders();
for (Provider p : providers) {
Set<Provider.Service> services = p.getServices();
for (Provider.Service service : services) {
if (!"KeyFactory".equals(service.getType())) {
continue;
}
if (!service.getAlgorithm().equals(keyAlgo)) {
continue;
}
if ("PKCS#8".equals(k.getFormat())) {
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(encoded);
KeyFactory kf = KeyFactory.getInstance(k.getAlgorithm(), p);
PrivateKey privKey = kf.generatePrivate(spec);
assertNotNull(k.getAlgorithm() + ", provider=" + p.getName(), privKey);
/*
* EC keys are unique because they can have explicit parameters or a curve
* name. Check them specially so this test can continue to function.
*/
if (k instanceof ECPrivateKey) {
assertECPrivateKeyEquals((ECPrivateKey) k, (ECPrivateKey) privKey);
} else {
assertEquals(k.getAlgorithm() + ", provider=" + p.getName(), Arrays.toString(encoded), Arrays.toString(privKey.getEncoded()));
}
} else if ("X.509".equals(k.getFormat())) {
X509EncodedKeySpec spec = new X509EncodedKeySpec(encoded);
KeyFactory kf = KeyFactory.getInstance(k.getAlgorithm(), p);
PublicKey pubKey = kf.generatePublic(spec);
assertNotNull(pubKey);
assertTrue(Arrays.equals(encoded, pubKey.getEncoded()));
}
}
}
}
Aggregations