use of com.google.crypto.tink.proto.EciesAeadHkdfParams in project tink by google.
the class EciesAeadHkdfPrivateKeyManagerTest method testNewKey.
@Test
public void testNewKey() throws Exception {
EllipticCurveType curve = EllipticCurveType.NIST_P384;
HashType hashType = HashType.SHA256;
EcPointFormat pointFormat = EcPointFormat.UNCOMPRESSED;
KeyTemplate demKeyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
byte[] salt = "some salt".getBytes("UTF-8");
EciesAeadHkdfParams params = HybridKeyTemplates.createEciesAeadHkdfParams(curve, hashType, pointFormat, demKeyTemplate, salt);
EciesAeadHkdfPrivateKeyManager manager = new EciesAeadHkdfPrivateKeyManager();
EciesAeadHkdfPrivateKey keyProto = (EciesAeadHkdfPrivateKey) manager.newKey(EciesAeadHkdfKeyFormat.newBuilder().setParams(params).build());
assertEquals(params, keyProto.getPublicKey().getParams());
Key primaryPriv = TestUtil.createKey(TestUtil.createKeyData(keyProto, EciesAeadHkdfPrivateKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 8, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key primaryPub = TestUtil.createKey(TestUtil.createKeyData(keyProto.getPublicKey(), EciesAeadHkdfPublicKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
KeysetHandle keysetHandlePub = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPub));
KeysetHandle keysetHandlePriv = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPriv));
HybridEncrypt hybridEncrypt = HybridEncryptFactory.getPrimitive(keysetHandlePub);
HybridDecrypt hybridDecrypt = HybridDecryptFactory.getPrimitive(keysetHandlePriv);
byte[] plaintext = Random.randBytes(20);
byte[] contextInfo = Random.randBytes(20);
byte[] ciphertext = hybridEncrypt.encrypt(plaintext, contextInfo);
assertArrayEquals(plaintext, hybridDecrypt.decrypt(ciphertext, contextInfo));
}
use of com.google.crypto.tink.proto.EciesAeadHkdfParams in project tink by google.
the class TestUtil method createEciesAeadHkdfPubKey.
/**
* @return a {@code EciesAeadHkdfPublicKey} with the specified key material and parameters.
*/
public static EciesAeadHkdfPublicKey createEciesAeadHkdfPubKey(EllipticCurveType curve, HashType hashType, EcPointFormat ecPointFormat, KeyTemplate demKeyTemplate, byte[] pubX, byte[] pubY, byte[] salt) throws Exception {
final int version = 0;
EciesAeadHkdfParams params = HybridKeyTemplates.createEciesAeadHkdfParams(curve, hashType, ecPointFormat, demKeyTemplate, salt);
return EciesAeadHkdfPublicKey.newBuilder().setVersion(version).setParams(params).setX(ByteString.copyFrom(pubX)).setY(ByteString.copyFrom(pubY)).build();
}
use of com.google.crypto.tink.proto.EciesAeadHkdfParams in project tink by google.
the class EciesAeadHkdfPrivateKeyManagerTest method createPrimitive.
@Test
public void createPrimitive() throws Exception {
EciesAeadHkdfPrivateKey key = createValidKey();
HybridDecrypt hybridDecrypt = manager.getPrimitive(key, HybridDecrypt.class);
EciesAeadHkdfParams eciesParams = key.getPublicKey().getParams();
EciesHkdfKemParams kemParams = eciesParams.getKemParams();
ECPublicKey recipientPublicKey = EllipticCurves.getEcPublicKey(HybridUtil.toCurveType(kemParams.getCurveType()), key.getPublicKey().getX().toByteArray(), key.getPublicKey().getY().toByteArray());
EciesAeadHkdfDemHelper demHelper = new RegistryEciesAeadHkdfDemHelper(eciesParams.getDemParams().getAeadDem());
HybridEncrypt hybridEncrypt = new EciesAeadHkdfHybridEncrypt(recipientPublicKey, kemParams.getHkdfSalt().toByteArray(), HybridUtil.toHmacAlgo(kemParams.getHkdfHashType()), HybridUtil.toPointFormatType(eciesParams.getEcPointFormat()), demHelper);
byte[] message = Random.randBytes(20);
byte[] contextInfo = Random.randBytes(20);
assertThat(hybridDecrypt.decrypt(hybridEncrypt.encrypt(message, contextInfo), contextInfo)).isEqualTo(message);
}
use of com.google.crypto.tink.proto.EciesAeadHkdfParams in project tink by google.
the class TestUtil method createEciesAeadHkdfPubKey.
/**
* @return a {@code EciesAeadHkdfPublicKey} with the specified key material and parameters.
*/
public static EciesAeadHkdfPublicKey createEciesAeadHkdfPubKey(EllipticCurveType curve, HashType hashType, EcPointFormat ecPointFormat, KeyTemplate demKeyTemplate, byte[] pubX, byte[] pubY, byte[] salt) throws Exception {
final int version = 0;
EciesAeadHkdfParams params = HybridKeyTemplates.createEciesAeadHkdfParams(curve, hashType, ecPointFormat, demKeyTemplate, salt);
return EciesAeadHkdfPublicKey.newBuilder().setVersion(version).setParams(params).setX(ByteString.copyFrom(pubX)).setY(ByteString.copyFrom(pubY)).build();
}
use of com.google.crypto.tink.proto.EciesAeadHkdfParams in project tink by google.
the class EciesAeadHkdfPrivateKeyManager method getPrimitive.
/**
* @param recipientKey {@code EciesAeadHkdfPrivateKey} proto
*/
@Override
public HybridDecrypt getPrimitive(MessageLite recipientKey) throws GeneralSecurityException {
if (!(recipientKey instanceof EciesAeadHkdfPrivateKey)) {
throw new GeneralSecurityException("expected EciesAeadHkdfPrivateKey proto");
}
EciesAeadHkdfPrivateKey recipientKeyProto = (EciesAeadHkdfPrivateKey) recipientKey;
validate(recipientKeyProto);
EciesAeadHkdfParams eciesParams = recipientKeyProto.getPublicKey().getParams();
EciesHkdfKemParams kemParams = eciesParams.getKemParams();
ECPrivateKey recipientPrivateKey = EllipticCurves.getEcPrivateKey(HybridUtil.toCurveType(kemParams.getCurveType()), recipientKeyProto.getKeyValue().toByteArray());
EciesAeadHkdfDemHelper demHelper = new RegistryEciesAeadHkdfDemHelper(eciesParams.getDemParams().getAeadDem());
return new EciesAeadHkdfHybridDecrypt(recipientPrivateKey, kemParams.getHkdfSalt().toByteArray(), HybridUtil.toHmacAlgo(kemParams.getHkdfHashType()), HybridUtil.toPointFormatType(eciesParams.getEcPointFormat()), demHelper);
}
Aggregations