use of com.google.crypto.tink.proto.EciesAeadHkdfPublicKey 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.EciesAeadHkdfPublicKey in project tink by google.
the class EciesAeadHkdfPublicKeyManagerTest method createPrimitive.
@Test
public void createPrimitive() throws Exception {
EciesAeadHkdfPrivateKey privateKey = createValidPrivateKey();
HybridDecrypt hybridDecrypt = privateManager.getPrimitive(privateKey, HybridDecrypt.class);
EciesAeadHkdfPublicKey publicKey = privateManager.getPublicKey(privateKey);
HybridEncrypt hybridEncrypt = publicManager.getPrimitive(publicKey, HybridEncrypt.class);
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.EciesAeadHkdfPublicKey in project tink by google.
the class EciesAeadHkdfPublicKeyManagerTest method validateKey_invalidWrongVersion_throws.
@Test
public void validateKey_invalidWrongVersion_throws() throws Exception {
EciesAeadHkdfPrivateKey privateKey = createValidPrivateKey();
EciesAeadHkdfPublicKey publicKey = privateManager.getPublicKey(privateKey);
EciesAeadHkdfPublicKey invalidKey = EciesAeadHkdfPublicKey.newBuilder().setVersion(1).build();
assertThrows(GeneralSecurityException.class, () -> publicManager.validateKey(invalidKey));
}
use of com.google.crypto.tink.proto.EciesAeadHkdfPublicKey in project tink by google.
the class TestUtil method generateEciesAeadHkdfPrivKey.
/**
* @return a freshly generated {@code EciesAeadHkdfPrivateKey} constructed with specified
* parameters.
*/
public static EciesAeadHkdfPrivateKey generateEciesAeadHkdfPrivKey(EllipticCurveType curve, HashType hashType, EcPointFormat pointFormat, KeyTemplate demKeyTemplate, byte[] salt) 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();
EciesAeadHkdfPublicKey eciesPubKey = createEciesAeadHkdfPubKey(curve, hashType, pointFormat, demKeyTemplate, w.getAffineX().toByteArray(), w.getAffineY().toByteArray(), salt);
return createEciesAeadHkdfPrivKey(eciesPubKey, privKey.getS().toByteArray());
}
use of com.google.crypto.tink.proto.EciesAeadHkdfPublicKey in project tink by google.
the class EciesAeadHkdfPublicKeyManager method getPrimitive.
/**
* @param recipientKey {@code EciesAeadHkdfPublicKey} proto
*/
@Override
public HybridEncrypt getPrimitive(MessageLite recipientKey) throws GeneralSecurityException {
if (!(recipientKey instanceof EciesAeadHkdfPublicKey)) {
throw new GeneralSecurityException("expected EciesAeadHkdfPublicKey proto");
}
EciesAeadHkdfPublicKey recipientKeyProto = (EciesAeadHkdfPublicKey) recipientKey;
validate(recipientKeyProto);
EciesAeadHkdfParams eciesParams = recipientKeyProto.getParams();
EciesHkdfKemParams kemParams = eciesParams.getKemParams();
ECPublicKey recipientPublicKey = EllipticCurves.getEcPublicKey(HybridUtil.toCurveType(kemParams.getCurveType()), recipientKeyProto.getX().toByteArray(), recipientKeyProto.getY().toByteArray());
EciesAeadHkdfDemHelper demHelper = new RegistryEciesAeadHkdfDemHelper(eciesParams.getDemParams().getAeadDem());
return new EciesAeadHkdfHybridEncrypt(recipientPublicKey, kemParams.getHkdfSalt().toByteArray(), HybridUtil.toHmacAlgo(kemParams.getHkdfHashType()), HybridUtil.toPointFormatType(eciesParams.getEcPointFormat()), demHelper);
}
Aggregations