use of com.google.crypto.tink.proto.EcdsaParams 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.EcdsaParams in project tink by google.
the class SignatureKeyTemplates method createEcdsaKeyTemplate.
/**
* @return a {@link KeyTemplate} containing a {@link HmacKeyFormat} with some specified
* parameters.
*/
public static KeyTemplate createEcdsaKeyTemplate(HashType hashType, EllipticCurveType curve, EcdsaSignatureEncoding encoding) {
EcdsaParams params = EcdsaParams.newBuilder().setHashType(hashType).setCurve(curve).setEncoding(encoding).build();
EcdsaKeyFormat format = EcdsaKeyFormat.newBuilder().setParams(params).build();
return KeyTemplate.newBuilder().setValue(format.toByteString()).setTypeUrl(EcdsaSignKeyManager.TYPE_URL).setOutputPrefixType(OutputPrefixType.TINK).build();
}
use of com.google.crypto.tink.proto.EcdsaParams in project tink by google.
the class TestUtil method createEcdsaPubKey.
/**
* @return a {@code EcdsaPublicKey} constructed from {@code HashType}, {@code EllipticCurveType}
* and affine coordinates of the public key.
*/
public static EcdsaPublicKey createEcdsaPubKey(HashType hashType, EllipticCurveType curve, EcdsaSignatureEncoding encoding, byte[] pubX, byte[] pubY) throws Exception {
final int version = 0;
EcdsaParams ecdsaParams = EcdsaParams.newBuilder().setHashType(hashType).setCurve(curve).setEncoding(encoding).build();
return EcdsaPublicKey.newBuilder().setVersion(version).setParams(ecdsaParams).setX(ByteString.copyFrom(pubX)).setY(ByteString.copyFrom(pubY)).build();
}
use of com.google.crypto.tink.proto.EcdsaParams in project tink by google.
the class EcdsaSignKeyManagerTest method testNewKeyUnsupportedEncoding.
@Test
public void testNewKeyUnsupportedEncoding() throws Exception {
EcdsaSignKeyManager signManager = new EcdsaSignKeyManager();
EcdsaParams ecdsaParams = EcdsaParams.newBuilder().setHashType(HashType.SHA256).setCurve(EllipticCurveType.NIST_P256).setEncoding(EcdsaSignatureEncoding.IEEE_P1363).build();
EcdsaKeyFormat ecdsaFormat = EcdsaKeyFormat.newBuilder().setParams(ecdsaParams).build();
try {
signManager.newKey(ecdsaFormat);
fail("Unsupported encoding, should have thrown exception");
} catch (GeneralSecurityException expecpted) {
// Raw encoding is not supported yet.
}
}
use of com.google.crypto.tink.proto.EcdsaParams in project tink by google.
the class EcdsaSignKeyManagerTest method testNewKeyUnsupportedKeyFormat.
private void testNewKeyUnsupportedKeyFormat(HashAndCurveType hashAndCurve) throws Exception {
HashType hashType = hashAndCurve.hashType;
EllipticCurveType curveType = hashAndCurve.curveType;
EcdsaSignKeyManager signManager = new EcdsaSignKeyManager();
EcdsaParams ecdsaParams = EcdsaParams.newBuilder().setHashType(hashType).setCurve(curveType).setEncoding(EcdsaSignatureEncoding.DER).build();
EcdsaKeyFormat ecdsaFormat = EcdsaKeyFormat.newBuilder().setParams(ecdsaParams).build();
try {
EcdsaPrivateKey unusedPrivKey = (EcdsaPrivateKey) signManager.newKey(ecdsaFormat);
fail("Unsupported key format, should have thrown exception: " + hashType + " " + curveType);
} catch (GeneralSecurityException expected) {
// Expected
}
}
Aggregations