use of com.google.crypto.tink.proto.EllipticCurveType in project tink by google.
the class HybridKeyTemplatesTest method testCreateEciesAeadHkdfKeyTemplate.
@Test
public void testCreateEciesAeadHkdfKeyTemplate() throws Exception {
// Intentionally using "weird" or invalid values for parameters,
// to test that the function correctly puts them in the resulting template.
EllipticCurveType curveType = EllipticCurveType.NIST_P384;
HashType hashType = HashType.SHA512;
EcPointFormat ecPointFormat = EcPointFormat.COMPRESSED;
KeyTemplate demKeyTemplate = AeadKeyTemplates.AES256_EAX;
String salt = "some salt";
KeyTemplate template = HybridKeyTemplates.createEciesAeadHkdfKeyTemplate(curveType, hashType, ecPointFormat, demKeyTemplate, salt.getBytes(UTF_8));
assertEquals(EciesAeadHkdfPrivateKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
EciesAeadHkdfKeyFormat format = EciesAeadHkdfKeyFormat.parseFrom(template.getValue());
assertTrue(format.hasParams());
assertTrue(format.getParams().hasKemParams());
assertTrue(format.getParams().hasDemParams());
assertTrue(format.getParams().getDemParams().hasAeadDem());
assertEquals(ecPointFormat, format.getParams().getEcPointFormat());
EciesHkdfKemParams kemParams = format.getParams().getKemParams();
assertEquals(curveType, kemParams.getCurveType());
assertEquals(hashType, kemParams.getHkdfHashType());
assertEquals(salt, kemParams.getHkdfSalt().toStringUtf8());
assertEquals(AeadKeyTemplates.AES256_EAX.toString(), format.getParams().getDemParams().getAeadDem().toString());
}
use of com.google.crypto.tink.proto.EllipticCurveType 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
}
}
use of com.google.crypto.tink.proto.EllipticCurveType in project tink by google.
the class EcdsaVerifyKeyManagerTest method testGetPrimitiveWithUnsupportedKey.
@Test
public void testGetPrimitiveWithUnsupportedKey() throws Exception {
HashAndCurveType[] hashAndCurves = { new HashAndCurveType(HashType.SHA1, EllipticCurveType.NIST_P256), new HashAndCurveType(HashType.SHA1, EllipticCurveType.NIST_P384), new HashAndCurveType(HashType.SHA1, EllipticCurveType.NIST_P521), new HashAndCurveType(HashType.SHA256, EllipticCurveType.NIST_P384), new HashAndCurveType(HashType.SHA256, EllipticCurveType.NIST_P521), new HashAndCurveType(HashType.SHA512, EllipticCurveType.NIST_P256) };
for (int i = 0; i < hashAndCurves.length; i++) {
HashType hashType = hashAndCurves[i].hashType;
EllipticCurveType curveType = hashAndCurves[i].curveType;
ECParameterSpec ecParams = EllipticCurves.getCurveSpec(SigUtil.toCurveType(curveType));
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(ecParams);
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey unusedPrivKey = (ECPrivateKey) keyPair.getPrivate();
// Create PublicKeyVerify.
ECPoint w = pubKey.getW();
try {
PublicKeyVerify unusedVerifier = createVerifier(hashType, curveType, EcdsaSignatureEncoding.DER, w.getAffineX().toByteArray(), w.getAffineY().toByteArray());
fail("Unsupported key, should have thrown exception: " + hashType + " " + curveType);
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
use of com.google.crypto.tink.proto.EllipticCurveType in project tink by google.
the class EcdsaVerifyKeyManagerTest method testGetPrimitiveWithJCE.
@Test
public void testGetPrimitiveWithJCE() throws Exception {
HashAndCurveType[] hashAndCurves = { new HashAndCurveType(HashType.SHA256, EllipticCurveType.NIST_P256), new HashAndCurveType(HashType.SHA512, EllipticCurveType.NIST_P384), new HashAndCurveType(HashType.SHA512, EllipticCurveType.NIST_P521) };
for (int i = 0; i < hashAndCurves.length; i++) {
HashType hashType = hashAndCurves[i].hashType;
EllipticCurveType curveType = hashAndCurves[i].curveType;
ECParameterSpec ecParams = EllipticCurves.getCurveSpec(SigUtil.toCurveType(curveType));
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(ecParams);
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privKey = (ECPrivateKey) keyPair.getPrivate();
// Sign with JCE's Signature.
Signature signer = Signature.getInstance(SigUtil.toEcdsaAlgo(hashType));
signer.initSign(privKey);
byte[] msg = Random.randBytes(1231);
signer.update(msg);
byte[] signature = signer.sign();
// Create PublicKeyVerify.
ECPoint w = pubKey.getW();
PublicKeyVerify verifier = createVerifier(hashType, curveType, EcdsaSignatureEncoding.DER, w.getAffineX().toByteArray(), w.getAffineY().toByteArray());
try {
verifier.verify(signature, msg);
} catch (GeneralSecurityException e) {
fail("Valid signature, should not throw exception");
}
}
}
Aggregations