Search in sources :

Example 1 with EcdsaKeyFormat

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();
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) EcdsaKeyFormat(com.google.crypto.tink.proto.EcdsaKeyFormat) EcdsaParams(com.google.crypto.tink.proto.EcdsaParams) ECPublicKey(java.security.interfaces.ECPublicKey) EcdsaPublicKey(com.google.crypto.tink.proto.EcdsaPublicKey) GeneralSecurityException(java.security.GeneralSecurityException) ECPoint(java.security.spec.ECPoint)

Example 2 with EcdsaKeyFormat

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();
}
Also used : EcdsaParams(com.google.crypto.tink.proto.EcdsaParams) EcdsaKeyFormat(com.google.crypto.tink.proto.EcdsaKeyFormat)

Example 3 with EcdsaKeyFormat

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());
}
Also used : EcdsaKeyFormat(com.google.crypto.tink.proto.EcdsaKeyFormat) HashType(com.google.crypto.tink.proto.HashType) EcdsaSignatureEncoding(com.google.crypto.tink.proto.EcdsaSignatureEncoding) EllipticCurveType(com.google.crypto.tink.proto.EllipticCurveType) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 4 with EcdsaKeyFormat

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());
}
Also used : EcdsaKeyFormat(com.google.crypto.tink.proto.EcdsaKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 5 with EcdsaKeyFormat

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());
}
Also used : EcdsaKeyFormat(com.google.crypto.tink.proto.EcdsaKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Aggregations

EcdsaKeyFormat (com.google.crypto.tink.proto.EcdsaKeyFormat)9 Test (org.junit.Test)5 EcdsaParams (com.google.crypto.tink.proto.EcdsaParams)4 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)4 GeneralSecurityException (java.security.GeneralSecurityException)4 EcdsaPrivateKey (com.google.crypto.tink.proto.EcdsaPrivateKey)2 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)2 HashType (com.google.crypto.tink.proto.HashType)2 ECPoint (java.security.spec.ECPoint)2 PublicKeySign (com.google.crypto.tink.PublicKeySign)1 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)1 EcdsaPublicKey (com.google.crypto.tink.proto.EcdsaPublicKey)1 EcdsaSignatureEncoding (com.google.crypto.tink.proto.EcdsaSignatureEncoding)1 ByteString (com.google.protobuf.ByteString)1 KeyPair (java.security.KeyPair)1 ECPrivateKey (java.security.interfaces.ECPrivateKey)1 ECPublicKey (java.security.interfaces.ECPublicKey)1 TreeSet (java.util.TreeSet)1