use of com.google.crypto.tink.proto.EcdsaKeyFormat 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.EcdsaKeyFormat 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.EcdsaKeyFormat in project tink by google.
the class SignatureKeyTemplatesTest method testCreateEcdsaKeyTemplate.
@Test
public void testCreateEcdsaKeyTemplate() throws Exception {
// Intentionally using "weird" or invalid values for parameters,
// to test that the function correctly puts them in the resulting template.
HashType hashType = HashType.SHA512;
EllipticCurveType curve = EllipticCurveType.NIST_P224;
EcdsaSignatureEncoding encoding = EcdsaSignatureEncoding.IEEE_P1363;
KeyTemplate template = SignatureKeyTemplates.createEcdsaKeyTemplate(hashType, curve, encoding);
assertEquals(EcdsaSignKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
EcdsaKeyFormat format = EcdsaKeyFormat.parseFrom(template.getValue());
assertEquals(hashType, format.getParams().getHashType());
assertEquals(curve, format.getParams().getCurve());
assertEquals(encoding, format.getParams().getEncoding());
}
use of com.google.crypto.tink.proto.EcdsaKeyFormat in project tink by google.
the class SignatureKeyTemplatesTest method testECDSA_P384.
@Test
public void testECDSA_P384() throws Exception {
KeyTemplate template = SignatureKeyTemplates.ECDSA_P384;
assertEquals(EcdsaSignKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
EcdsaKeyFormat format = EcdsaKeyFormat.parseFrom(template.getValue());
assertTrue(format.hasParams());
assertEquals(HashType.SHA512, format.getParams().getHashType());
assertEquals(EllipticCurveType.NIST_P384, format.getParams().getCurve());
assertEquals(EcdsaSignatureEncoding.DER, format.getParams().getEncoding());
}
use of com.google.crypto.tink.proto.EcdsaKeyFormat in project tink by google.
the class SignatureKeyTemplatesTest method testECDSA_P521.
@Test
public void testECDSA_P521() throws Exception {
KeyTemplate template = SignatureKeyTemplates.ECDSA_P521;
assertEquals(EcdsaSignKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
EcdsaKeyFormat format = EcdsaKeyFormat.parseFrom(template.getValue());
assertTrue(format.hasParams());
assertEquals(HashType.SHA512, format.getParams().getHashType());
assertEquals(EllipticCurveType.NIST_P521, format.getParams().getCurve());
assertEquals(EcdsaSignatureEncoding.DER, format.getParams().getEncoding());
}
Aggregations