use of com.google.crypto.tink.proto.RsaSsaPssKeyFormat in project tink by google.
the class RsaSsaPssSignKeyManagerTest method validateKeyFormat_smallModulusDisallowed_throws.
@Test
public void validateKeyFormat_smallModulusDisallowed_throws() throws Exception {
RsaSsaPssKeyFormat format = createKeyFormat(HashType.SHA256, HashType.SHA256, 32, 512, RSAKeyGenParameterSpec.F4);
assertThrows(GeneralSecurityException.class, () -> factory.validateKeyFormat(format));
}
use of com.google.crypto.tink.proto.RsaSsaPssKeyFormat in project tink by google.
the class RsaSsaPssSignKeyManagerTest method validateKeyFormat_unkownHashDisallowed_throws.
@Test
public void validateKeyFormat_unkownHashDisallowed_throws() throws Exception {
RsaSsaPssKeyFormat format = createKeyFormat(HashType.UNKNOWN_HASH, HashType.UNKNOWN_HASH, 32, 3072, RSAKeyGenParameterSpec.F4);
assertThrows(GeneralSecurityException.class, () -> factory.validateKeyFormat(format));
}
use of com.google.crypto.tink.proto.RsaSsaPssKeyFormat in project tink by google.
the class RsaSsaPssSignKeyManagerTest method testRawRsa4096PssSha512F4TemplateWithManager.
@Test
public void testRawRsa4096PssSha512F4TemplateWithManager() throws Exception {
RsaSsaPssKeyFormat format = RsaSsaPssKeyFormat.parseFrom(RsaSsaPssSignKeyManager.rawRsa4096PssSha512F4Template().getValue(), ExtensionRegistryLite.getEmptyRegistry());
new RsaSsaPssSignKeyManager().keyFactory().validateKeyFormat(format);
}
use of com.google.crypto.tink.proto.RsaSsaPssKeyFormat in project tink by google.
the class RsaSsaPssSignKeyManagerTest method validateKeyFormat_sha512Allowed.
@Test
public void validateKeyFormat_sha512Allowed() throws Exception {
RsaSsaPssKeyFormat format = createKeyFormat(HashType.SHA512, HashType.SHA512, 32, 3072, RSAKeyGenParameterSpec.F4);
factory.validateKeyFormat(format);
}
use of com.google.crypto.tink.proto.RsaSsaPssKeyFormat in project tink by google.
the class RsaSsaPssSignKeyManager method keyFactory.
@Override
public KeyFactory<RsaSsaPssKeyFormat, RsaSsaPssPrivateKey> keyFactory() {
return new KeyFactory<RsaSsaPssKeyFormat, RsaSsaPssPrivateKey>(RsaSsaPssKeyFormat.class) {
@Override
public void validateKeyFormat(RsaSsaPssKeyFormat format) throws GeneralSecurityException {
SigUtil.validateRsaSsaPssParams(format.getParams());
Validators.validateRsaModulusSize(format.getModulusSizeInBits());
Validators.validateRsaPublicExponent(new BigInteger(1, format.getPublicExponent().toByteArray()));
}
@Override
public RsaSsaPssKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException {
return RsaSsaPssKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
}
@Override
public RsaSsaPssPrivateKey createKey(RsaSsaPssKeyFormat format) throws GeneralSecurityException {
RsaSsaPssParams params = format.getParams();
Validators.validateRsaModulusSize(format.getModulusSizeInBits());
Validators.validateSignatureHash(SigUtil.toHashType(params.getSigHash()));
KeyPairGenerator keyGen = EngineFactory.KEY_PAIR_GENERATOR.getInstance("RSA");
RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(format.getModulusSizeInBits(), new BigInteger(1, format.getPublicExponent().toByteArray()));
keyGen.initialize(spec);
KeyPair keyPair = keyGen.generateKeyPair();
RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateCrtKey privKey = (RSAPrivateCrtKey) keyPair.getPrivate();
// Creates RsaSsaPssPublicKey.
RsaSsaPssPublicKey pssPubKey = RsaSsaPssPublicKey.newBuilder().setVersion(getVersion()).setParams(params).setE(ByteString.copyFrom(pubKey.getPublicExponent().toByteArray())).setN(ByteString.copyFrom(pubKey.getModulus().toByteArray())).build();
// Creates RsaSsaPssPrivateKey.
return RsaSsaPssPrivateKey.newBuilder().setVersion(getVersion()).setPublicKey(pssPubKey).setD(ByteString.copyFrom(privKey.getPrivateExponent().toByteArray())).setP(ByteString.copyFrom(privKey.getPrimeP().toByteArray())).setQ(ByteString.copyFrom(privKey.getPrimeQ().toByteArray())).setDp(ByteString.copyFrom(privKey.getPrimeExponentP().toByteArray())).setDq(ByteString.copyFrom(privKey.getPrimeExponentQ().toByteArray())).setCrt(ByteString.copyFrom(privKey.getCrtCoefficient().toByteArray())).build();
}
@Override
public Map<String, KeyFactory.KeyFormat<RsaSsaPssKeyFormat>> keyFormats() throws GeneralSecurityException {
Map<String, KeyFactory.KeyFormat<RsaSsaPssKeyFormat>> result = new HashMap<>();
result.put("RSA_SSA_PSS_3072_SHA256_F4", new KeyFormat<>(createKeyFormat(HashType.SHA256, HashType.SHA256, /*saltLength=*/
32, /*modulusSize=*/
3072, RSAKeyGenParameterSpec.F4), KeyTemplate.OutputPrefixType.TINK));
result.put("RSA_SSA_PSS_3072_SHA256_F4_RAW", new KeyFormat<>(createKeyFormat(HashType.SHA256, HashType.SHA256, /*saltLength=*/
32, /*modulusSize=*/
3072, RSAKeyGenParameterSpec.F4), KeyTemplate.OutputPrefixType.RAW));
// This is identical to RSA_SSA_PSS_3072_SHA256_F4. It is needed to maintain backward
// compatibility with SignatureKeyTemplates.
// TODO(b/185475349): remove this in Tink 2.0.0.
result.put("RSA_SSA_PSS_3072_SHA256_SHA256_32_F4", new KeyFormat<>(createKeyFormat(HashType.SHA256, HashType.SHA256, /*saltLength=*/
32, /*modulusSize=*/
3072, RSAKeyGenParameterSpec.F4), KeyTemplate.OutputPrefixType.TINK));
result.put("RSA_SSA_PSS_4096_SHA512_F4", new KeyFormat<>(createKeyFormat(HashType.SHA512, HashType.SHA512, /*saltLength=*/
64, /*modulusSize=*/
4096, RSAKeyGenParameterSpec.F4), KeyTemplate.OutputPrefixType.TINK));
result.put("RSA_SSA_PSS_4096_SHA512_F4_RAW", new KeyFormat<>(createKeyFormat(HashType.SHA512, HashType.SHA512, /*saltLength=*/
64, /*modulusSize=*/
4096, RSAKeyGenParameterSpec.F4), KeyTemplate.OutputPrefixType.RAW));
// This is identical to RSA_SSA_PSS_4096_SHA512_F4. It is needed to maintain backward
// compatibility with SignatureKeyTemplates.
// TODO(b/185475349): remove this in Tink 2.0.0.
result.put("RSA_SSA_PSS_4096_SHA512_SHA512_64_F4", new KeyFormat<>(createKeyFormat(HashType.SHA512, HashType.SHA512, /*saltLength=*/
64, /*modulusSize=*/
4096, RSAKeyGenParameterSpec.F4), KeyTemplate.OutputPrefixType.TINK));
return Collections.unmodifiableMap(result);
}
};
}
Aggregations