use of com.google.crypto.tink.proto.EcdsaPublicKey in project tink by google.
the class EcdsaSignKeyManager method newKey.
/**
* @param keyFormat {@code EcdsaKeyFormat} proto
* @return new {@code EcdsaPrivateKey} proto
*/
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
if (!(keyFormat instanceof EcdsaKeyFormat)) {
throw new GeneralSecurityException("expected EcdsaKeyFormat proto");
}
EcdsaKeyFormat format = (EcdsaKeyFormat) keyFormat;
EcdsaParams ecdsaParams = format.getParams();
SigUtil.validateEcdsaParams(ecdsaParams);
KeyPair keyPair = EllipticCurves.generateKeyPair(SigUtil.toCurveType(ecdsaParams.getCurve()));
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privKey = (ECPrivateKey) keyPair.getPrivate();
ECPoint w = pubKey.getW();
// Creates EcdsaPublicKey.
EcdsaPublicKey ecdsaPubKey = EcdsaPublicKey.newBuilder().setVersion(VERSION).setParams(ecdsaParams).setX(ByteString.copyFrom(w.getAffineX().toByteArray())).setY(ByteString.copyFrom(w.getAffineY().toByteArray())).build();
// Creates EcdsaPrivateKey.
return EcdsaPrivateKey.newBuilder().setVersion(VERSION).setPublicKey(ecdsaPubKey).setKeyValue(ByteString.copyFrom(privKey.getS().toByteArray())).build();
}
use of com.google.crypto.tink.proto.EcdsaPublicKey in project tink by google.
the class TestUtil method generateEcdsaPrivKey.
/**
* @return a {@code EcdsaPrivateKey} constructed from {@code EllipticCurveType} and {@code
* HashType}.
*/
public static EcdsaPrivateKey generateEcdsaPrivKey(EllipticCurveType curve, HashType hashType, EcdsaSignatureEncoding encoding) throws Exception {
ECParameterSpec ecParams;
switch(curve) {
case NIST_P256:
ecParams = EllipticCurves.getNistP256Params();
break;
case NIST_P384:
ecParams = EllipticCurves.getNistP384Params();
break;
case NIST_P521:
ecParams = EllipticCurves.getNistP521Params();
break;
default:
throw new NoSuchAlgorithmException("Curve not implemented:" + curve);
}
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(ecParams);
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privKey = (ECPrivateKey) keyPair.getPrivate();
ECPoint w = pubKey.getW();
EcdsaPublicKey ecdsaPubKey = createEcdsaPubKey(hashType, curve, encoding, w.getAffineX().toByteArray(), w.getAffineY().toByteArray());
return createEcdsaPrivKey(ecdsaPubKey, privKey.getS().toByteArray());
}
use of com.google.crypto.tink.proto.EcdsaPublicKey in project tink by google.
the class EcdsaVerifyKeyManagerTest method createVerifier.
private PublicKeyVerify createVerifier(HashType hashType, EllipticCurveType curve, EcdsaSignatureEncoding encoding, byte[] pubX, byte[] pubY) throws Exception {
EcdsaPublicKey ecdsaPubKey = TestUtil.createEcdsaPubKey(hashType, curve, encoding, pubX, pubY);
EcdsaVerifyKeyManager verifyManager = new EcdsaVerifyKeyManager();
return verifyManager.getPrimitive(ecdsaPubKey);
}
use of com.google.crypto.tink.proto.EcdsaPublicKey in project tink by google.
the class EcdsaSignKeyManagerTest method testGetPrimitiveWithUnsupportedKey.
private void testGetPrimitiveWithUnsupportedKey(HashAndCurveType hashAndCurve) throws Exception {
HashType hashType = hashAndCurve.hashType;
EllipticCurveType curveType = hashAndCurve.curveType;
KeyPair keyPair = EllipticCurves.generateKeyPair(SigUtil.toCurveType(curveType));
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privKey = (ECPrivateKey) keyPair.getPrivate();
ECPoint w = pubKey.getW();
EcdsaPublicKey ecdsaPubKey = TestUtil.createEcdsaPubKey(hashType, curveType, EcdsaSignatureEncoding.DER, w.getAffineX().toByteArray(), w.getAffineY().toByteArray());
EcdsaPrivateKey ecdsaPrivKey = TestUtil.createEcdsaPrivKey(ecdsaPubKey, privKey.getS().toByteArray());
EcdsaSignKeyManager signManager = new EcdsaSignKeyManager();
try {
PublicKeySign unusedSigner = signManager.getPrimitive(ecdsaPrivKey);
fail("Unsupported key, should have thrown exception: " + hashType + " " + curveType);
} catch (GeneralSecurityException expected) {
// Expected
}
}
use of com.google.crypto.tink.proto.EcdsaPublicKey in project tink by google.
the class EcdsaProtoTest method testKeysetBasic.
@Test
public void testKeysetBasic() throws Exception {
EcdsaPublicKey publicKey = EcdsaPublicKey.newBuilder().setVersion(1).build();
assertEquals(1, publicKey.getVersion());
}
Aggregations