package com.google.crypto.tink.signature;
import com.google.crypto.tink.KeyTypeManager;
import com.google.crypto.tink.PublicKeyVerify;
import com.google.crypto.tink.proto.KeyData.KeyMaterialType;
import com.google.crypto.tink.proto.RsaSsaPssParams;
import com.google.crypto.tink.proto.RsaSsaPssPublicKey;
import com.google.crypto.tink.subtle.EngineFactory;
import com.google.crypto.tink.subtle.RsaSsaPssVerifyJce;
import com.google.crypto.tink.subtle.Validators;
import com.google.protobuf.ByteString;
import com.google.protobuf.ExtensionRegistryLite;
import com.google.protobuf.InvalidProtocolBufferException;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPublicKeySpec;
class RsaSsaPssVerifyKeyManager extends KeyTypeManager<RsaSsaPssPublicKey> {
public RsaSsaPssVerifyKeyManager() {
super(
RsaSsaPssPublicKey.class,
new PrimitiveFactory<PublicKeyVerify, RsaSsaPssPublicKey>(PublicKeyVerify.class) {
@Override
public PublicKeyVerify getPrimitive(RsaSsaPssPublicKey keyProto)
throws GeneralSecurityException {
java.security.KeyFactory kf = EngineFactory.KEY_FACTORY.getInstance("RSA");
BigInteger modulus = new BigInteger(1, keyProto.getN().toByteArray());
BigInteger exponent = new BigInteger(1, keyProto.getE().toByteArray());
RSAPublicKey publicKey =
(RSAPublicKey) kf.generatePublic(new RSAPublicKeySpec(modulus, exponent));
RsaSsaPssParams params = keyProto.getParams();
return new RsaSsaPssVerifyJce(
publicKey,
SigUtil.toHashType(params.getSigHash()),
SigUtil.toHashType(params.getMgf1Hash()),
params.getSaltLength());
}
});
}
@Override
public String getKeyType() {
return "type.googleapis.com/google.crypto.tink.RsaSsaPssPublicKey";
}
@Override
public int getVersion() {
return 0;
}
@Override
public KeyMaterialType keyMaterialType() {
return KeyMaterialType.ASYMMETRIC_PUBLIC;
}
@Override
public RsaSsaPssPublicKey parseKey(ByteString byteString) throws InvalidProtocolBufferException {
return RsaSsaPssPublicKey.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
}
@Override
public void validateKey(RsaSsaPssPublicKey pubKey) throws GeneralSecurityException {
Validators.validateVersion(pubKey.getVersion(), getVersion());
Validators.validateRsaModulusSize(new BigInteger(1, pubKey.getN().toByteArray()).bitLength());
SigUtil.validateRsaSsaPssParams(pubKey.getParams());
}
}