Search in sources :

Example 1 with EcdsaPublicKey

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

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

Example 3 with EcdsaPublicKey

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

Example 4 with EcdsaPublicKey

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

Example 5 with EcdsaPublicKey

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

Aggregations

EcdsaPublicKey (com.google.crypto.tink.proto.EcdsaPublicKey)6 ECPublicKey (java.security.interfaces.ECPublicKey)4 GeneralSecurityException (java.security.GeneralSecurityException)3 KeyPair (java.security.KeyPair)3 ECPrivateKey (java.security.interfaces.ECPrivateKey)3 ECPoint (java.security.spec.ECPoint)3 PublicKeySign (com.google.crypto.tink.PublicKeySign)1 EcdsaKeyFormat (com.google.crypto.tink.proto.EcdsaKeyFormat)1 EcdsaParams (com.google.crypto.tink.proto.EcdsaParams)1 EcdsaPrivateKey (com.google.crypto.tink.proto.EcdsaPrivateKey)1 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)1 HashType (com.google.crypto.tink.proto.HashType)1 EcdsaVerifyJce (com.google.crypto.tink.subtle.EcdsaVerifyJce)1 KeyPairGenerator (java.security.KeyPairGenerator)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ECParameterSpec (java.security.spec.ECParameterSpec)1 Test (org.junit.Test)1