Search in sources :

Example 11 with EciesHkdfKemParams

use of com.google.crypto.tink.proto.EciesHkdfKemParams 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);
}
Also used : EciesAeadHkdfDemHelper(com.google.crypto.tink.subtle.EciesAeadHkdfDemHelper) EciesAeadHkdfParams(com.google.crypto.tink.proto.EciesAeadHkdfParams) ECPublicKey(java.security.interfaces.ECPublicKey) GeneralSecurityException(java.security.GeneralSecurityException) EciesAeadHkdfPublicKey(com.google.crypto.tink.proto.EciesAeadHkdfPublicKey) EciesHkdfKemParams(com.google.crypto.tink.proto.EciesHkdfKemParams) EciesAeadHkdfHybridEncrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)

Example 12 with EciesHkdfKemParams

use of com.google.crypto.tink.proto.EciesHkdfKemParams in project tink by google.

the class EciesAeadHkdfPrivateKeyManager method keyFactory.

@Override
public KeyFactory<EciesAeadHkdfKeyFormat, EciesAeadHkdfPrivateKey> keyFactory() {
    return new KeyFactory<EciesAeadHkdfKeyFormat, EciesAeadHkdfPrivateKey>(EciesAeadHkdfKeyFormat.class) {

        @Override
        public void validateKeyFormat(EciesAeadHkdfKeyFormat eciesKeyFormat) throws GeneralSecurityException {
            HybridUtil.validate(eciesKeyFormat.getParams());
        }

        @Override
        public EciesAeadHkdfKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException {
            return EciesAeadHkdfKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
        }

        @Override
        public EciesAeadHkdfPrivateKey createKey(EciesAeadHkdfKeyFormat eciesKeyFormat) throws GeneralSecurityException {
            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(getVersion()).setParams(eciesKeyFormat.getParams()).setX(ByteString.copyFrom(w.getAffineX().toByteArray())).setY(ByteString.copyFrom(w.getAffineY().toByteArray())).build();
            // Creates EciesAeadHkdfPrivateKey.
            return EciesAeadHkdfPrivateKey.newBuilder().setVersion(getVersion()).setPublicKey(eciesPublicKey).setKeyValue(ByteString.copyFrom(privKey.getS().toByteArray())).build();
        }

        @Override
        public Map<String, KeyFactory.KeyFormat<EciesAeadHkdfKeyFormat>> keyFormats() throws GeneralSecurityException {
            Map<String, KeyFactory.KeyFormat<EciesAeadHkdfKeyFormat>> result = new HashMap<>();
            result.put("ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM", createKeyFormat(EllipticCurveType.NIST_P256, HashType.SHA256, EcPointFormat.UNCOMPRESSED, KeyTemplates.get("AES128_GCM"), EMPTY_SALT, KeyTemplate.OutputPrefixType.TINK));
            result.put("ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM_RAW", createKeyFormat(EllipticCurveType.NIST_P256, HashType.SHA256, EcPointFormat.UNCOMPRESSED, KeyTemplates.get("AES128_GCM"), EMPTY_SALT, KeyTemplate.OutputPrefixType.RAW));
            result.put("ECIES_P256_COMPRESSED_HKDF_HMAC_SHA256_AES128_GCM", createKeyFormat(EllipticCurveType.NIST_P256, HashType.SHA256, EcPointFormat.COMPRESSED, KeyTemplates.get("AES128_GCM"), EMPTY_SALT, KeyTemplate.OutputPrefixType.TINK));
            result.put("ECIES_P256_COMPRESSED_HKDF_HMAC_SHA256_AES128_GCM_RAW", createKeyFormat(EllipticCurveType.NIST_P256, HashType.SHA256, EcPointFormat.COMPRESSED, KeyTemplates.get("AES128_GCM"), EMPTY_SALT, KeyTemplate.OutputPrefixType.RAW));
            // backward compatibility with HybridKeyTemplates
            result.put("ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM_COMPRESSED_WITHOUT_PREFIX", createKeyFormat(EllipticCurveType.NIST_P256, HashType.SHA256, EcPointFormat.COMPRESSED, KeyTemplates.get("AES128_GCM"), EMPTY_SALT, KeyTemplate.OutputPrefixType.RAW));
            result.put("ECIES_P256_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256", createKeyFormat(EllipticCurveType.NIST_P256, HashType.SHA256, EcPointFormat.UNCOMPRESSED, KeyTemplates.get("AES128_CTR_HMAC_SHA256"), EMPTY_SALT, KeyTemplate.OutputPrefixType.TINK));
            result.put("ECIES_P256_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256_RAW", createKeyFormat(EllipticCurveType.NIST_P256, HashType.SHA256, EcPointFormat.UNCOMPRESSED, KeyTemplates.get("AES128_CTR_HMAC_SHA256"), EMPTY_SALT, KeyTemplate.OutputPrefixType.RAW));
            result.put("ECIES_P256_COMPRESSED_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256", createKeyFormat(EllipticCurveType.NIST_P256, HashType.SHA256, EcPointFormat.COMPRESSED, KeyTemplates.get("AES128_CTR_HMAC_SHA256"), EMPTY_SALT, KeyTemplate.OutputPrefixType.TINK));
            result.put("ECIES_P256_COMPRESSED_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256_RAW", createKeyFormat(EllipticCurveType.NIST_P256, HashType.SHA256, EcPointFormat.COMPRESSED, KeyTemplates.get("AES128_CTR_HMAC_SHA256"), EMPTY_SALT, KeyTemplate.OutputPrefixType.RAW));
            return Collections.unmodifiableMap(result);
        }
    };
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) EciesAeadHkdfKeyFormat(com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ECPoint(java.security.spec.ECPoint) EciesAeadHkdfKeyFormat(com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat) EciesHkdfKemParams(com.google.crypto.tink.proto.EciesHkdfKemParams) ECPublicKey(java.security.interfaces.ECPublicKey) EciesAeadHkdfPublicKey(com.google.crypto.tink.proto.EciesAeadHkdfPublicKey)

Example 13 with EciesHkdfKemParams

use of com.google.crypto.tink.proto.EciesHkdfKemParams in project tink by google.

the class EciesAeadHkdfPrivateKeyManager method createParams.

/**
 * @return a {@link EciesAeadHkdfParams} with the specified parameters.
 */
static EciesAeadHkdfParams createParams(EllipticCurveType curve, HashType hashType, EcPointFormat ecPointFormat, KeyTemplate demKeyTemplate, byte[] salt) {
    EciesHkdfKemParams kemParams = EciesHkdfKemParams.newBuilder().setCurveType(curve).setHkdfHashType(hashType).setHkdfSalt(ByteString.copyFrom(salt)).build();
    com.google.crypto.tink.proto.KeyTemplate protoKt = com.google.crypto.tink.proto.KeyTemplate.newBuilder().setTypeUrl(demKeyTemplate.getTypeUrl()).setValue(ByteString.copyFrom(demKeyTemplate.getValue())).setOutputPrefixType(toProto(demKeyTemplate.getOutputPrefixType())).build();
    EciesAeadDemParams demParams = EciesAeadDemParams.newBuilder().setAeadDem(protoKt).build();
    return EciesAeadHkdfParams.newBuilder().setKemParams(kemParams).setDemParams(demParams).setEcPointFormat(ecPointFormat).build();
}
Also used : EciesAeadDemParams(com.google.crypto.tink.proto.EciesAeadDemParams) EciesHkdfKemParams(com.google.crypto.tink.proto.EciesHkdfKemParams)

Example 14 with EciesHkdfKemParams

use of com.google.crypto.tink.proto.EciesHkdfKemParams in project tink by google.

the class HybridKeyTemplates method createEciesAeadHkdfParams.

/**
 * @return a {@link EciesAeadHkdfParams} with the specified parameters.
 */
public static EciesAeadHkdfParams createEciesAeadHkdfParams(EllipticCurveType curve, HashType hashType, EcPointFormat ecPointFormat, KeyTemplate demKeyTemplate, byte[] salt) {
    EciesHkdfKemParams kemParams = EciesHkdfKemParams.newBuilder().setCurveType(curve).setHkdfHashType(hashType).setHkdfSalt(ByteString.copyFrom(salt)).build();
    EciesAeadDemParams demParams = EciesAeadDemParams.newBuilder().setAeadDem(demKeyTemplate).build();
    return EciesAeadHkdfParams.newBuilder().setKemParams(kemParams).setDemParams(demParams).setEcPointFormat(ecPointFormat).build();
}
Also used : EciesAeadDemParams(com.google.crypto.tink.proto.EciesAeadDemParams) EciesHkdfKemParams(com.google.crypto.tink.proto.EciesHkdfKemParams)

Example 15 with EciesHkdfKemParams

use of com.google.crypto.tink.proto.EciesHkdfKemParams in project tink by google.

the class EciesAeadHkdfPublicKeyManagerTest method createKeyFormat.

private EciesAeadHkdfKeyFormat createKeyFormat(EllipticCurveType curve, HashType hashType, EcPointFormat ecPointFormat, KeyTemplate demKeyTemplate, byte[] salt) {
    EciesHkdfKemParams kemParams = EciesHkdfKemParams.newBuilder().setCurveType(curve).setHkdfHashType(hashType).setHkdfSalt(ByteString.copyFrom(salt)).build();
    EciesAeadDemParams demParams = EciesAeadDemParams.newBuilder().setAeadDem(demKeyTemplate).build();
    EciesAeadHkdfParams params = EciesAeadHkdfParams.newBuilder().setKemParams(kemParams).setDemParams(demParams).setEcPointFormat(ecPointFormat).build();
    return EciesAeadHkdfKeyFormat.newBuilder().setParams(params).build();
}
Also used : EciesAeadHkdfParams(com.google.crypto.tink.proto.EciesAeadHkdfParams) EciesAeadDemParams(com.google.crypto.tink.proto.EciesAeadDemParams) EciesHkdfKemParams(com.google.crypto.tink.proto.EciesHkdfKemParams)

Aggregations

EciesHkdfKemParams (com.google.crypto.tink.proto.EciesHkdfKemParams)18 EciesAeadHkdfKeyFormat (com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat)12 Test (org.junit.Test)11 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)6 KeyTemplate (com.google.crypto.tink.KeyTemplate)4 EciesAeadHkdfParams (com.google.crypto.tink.proto.EciesAeadHkdfParams)4 ECPublicKey (java.security.interfaces.ECPublicKey)4 EciesAeadDemParams (com.google.crypto.tink.proto.EciesAeadDemParams)3 EciesAeadHkdfPublicKey (com.google.crypto.tink.proto.EciesAeadHkdfPublicKey)3 EciesAeadHkdfDemHelper (com.google.crypto.tink.subtle.EciesAeadHkdfDemHelper)3 GeneralSecurityException (java.security.GeneralSecurityException)3 ECPrivateKey (java.security.interfaces.ECPrivateKey)3 EciesAeadHkdfPrivateKey (com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey)2 EciesAeadHkdfHybridEncrypt (com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)2 KeyPair (java.security.KeyPair)2 ECPoint (java.security.spec.ECPoint)2 HybridDecrypt (com.google.crypto.tink.HybridDecrypt)1 HybridEncrypt (com.google.crypto.tink.HybridEncrypt)1 EcPointFormat (com.google.crypto.tink.proto.EcPointFormat)1 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)1