use of com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat in project tink by google.
the class EciesAeadHkdfPrivateKeyManager method newKey.
/**
* @param keyFormat {@code EciesAeadHkdfKeyFormat} proto
* @return new {@code EciesAeadHkdfPrivateKey} proto
*/
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
if (!(keyFormat instanceof EciesAeadHkdfKeyFormat)) {
throw new GeneralSecurityException("expected EciesAeadHkdfKeyFormat proto");
}
EciesAeadHkdfKeyFormat eciesKeyFormat = (EciesAeadHkdfKeyFormat) keyFormat;
HybridUtil.validate(eciesKeyFormat.getParams());
EciesHkdfKemParams kemParams = eciesKeyFormat.getParams().getKemParams();
KeyPair keyPair = EllipticCurves.generateKeyPair(HybridUtil.toCurveType(kemParams.getCurveType()));
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privKey = (ECPrivateKey) keyPair.getPrivate();
ECPoint w = pubKey.getW();
// Creates EciesAeadHkdfPublicKey.
EciesAeadHkdfPublicKey eciesPublicKey = EciesAeadHkdfPublicKey.newBuilder().setVersion(VERSION).setParams(eciesKeyFormat.getParams()).setX(ByteString.copyFrom(w.getAffineX().toByteArray())).setY(ByteString.copyFrom(w.getAffineY().toByteArray())).build();
// Creates EciesAeadHkdfPrivateKey.
return EciesAeadHkdfPrivateKey.newBuilder().setVersion(VERSION).setPublicKey(eciesPublicKey).setKeyValue(ByteString.copyFrom(privKey.getS().toByteArray())).build();
}
use of com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat in project tink by google.
the class HybridKeyTemplatesTest method testECIES_P256_HKDF_HMAC_SHA256_AES128_GCM.
@Test
public void testECIES_P256_HKDF_HMAC_SHA256_AES128_GCM() throws Exception {
KeyTemplate template = HybridKeyTemplates.ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM;
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.UNCOMPRESSED, format.getParams().getEcPointFormat());
EciesHkdfKemParams kemParams = format.getParams().getKemParams();
assertEquals(EllipticCurveType.NIST_P256, kemParams.getCurveType());
assertEquals(HashType.SHA256, kemParams.getHkdfHashType());
assertTrue(kemParams.getHkdfSalt().isEmpty());
assertEquals(AeadKeyTemplates.AES128_GCM.toString(), format.getParams().getDemParams().getAeadDem().toString());
}
use of com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat in project tink by google.
the class HybridKeyTemplatesTest method testECIES_P256_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256.
@Test
public void testECIES_P256_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256() throws Exception {
KeyTemplate template = HybridKeyTemplates.ECIES_P256_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256;
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.UNCOMPRESSED, format.getParams().getEcPointFormat());
EciesHkdfKemParams kemParams = format.getParams().getKemParams();
assertEquals(EllipticCurveType.NIST_P256, kemParams.getCurveType());
assertEquals(HashType.SHA256, kemParams.getHkdfHashType());
assertTrue(kemParams.getHkdfSalt().isEmpty());
assertEquals(AeadKeyTemplates.AES128_CTR_HMAC_SHA256.toString(), format.getParams().getDemParams().getAeadDem().toString());
}
use of com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat 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());
}
Aggregations