Search in sources :

Example 1 with EcdsaParams

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();
}
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 EcdsaParams

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

Example 3 with EcdsaParams

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

Example 4 with EcdsaParams

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

Example 5 with EcdsaParams

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
    }
}
Also used : EcdsaParams(com.google.crypto.tink.proto.EcdsaParams) EcdsaKeyFormat(com.google.crypto.tink.proto.EcdsaKeyFormat) HashType(com.google.crypto.tink.proto.HashType) GeneralSecurityException(java.security.GeneralSecurityException) EllipticCurveType(com.google.crypto.tink.proto.EllipticCurveType) EcdsaPrivateKey(com.google.crypto.tink.proto.EcdsaPrivateKey)

Aggregations

EcdsaParams (com.google.crypto.tink.proto.EcdsaParams)5 EcdsaKeyFormat (com.google.crypto.tink.proto.EcdsaKeyFormat)4 GeneralSecurityException (java.security.GeneralSecurityException)3 ECPoint (java.security.spec.ECPoint)2 EcdsaPrivateKey (com.google.crypto.tink.proto.EcdsaPrivateKey)1 EcdsaPublicKey (com.google.crypto.tink.proto.EcdsaPublicKey)1 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)1 HashType (com.google.crypto.tink.proto.HashType)1 KeyPair (java.security.KeyPair)1 ECPrivateKey (java.security.interfaces.ECPrivateKey)1 ECPublicKey (java.security.interfaces.ECPublicKey)1 Test (org.junit.Test)1