com.akeyless.crypto.rsa.RsaMpcSign Maven / Gradle / Ivy
package com.akeyless.crypto.rsa;
import com.akeyless.crypto.rsa.internal.JCAUtil;
import com.akeyless.crypto.rsa.internal.RSAPadding;
import sun.security.rsa.RSASignature;
import sun.security.util.ObjectIdentifier;
import sun.security.x509.AlgorithmId;
import javax.crypto.BadPaddingException;
import java.io.IOException;
import java.math.BigInteger;
import java.security.*;
import java.util.List;
public class RsaMpcSign {
public static byte[] preSignPKCS1v15(byte[] msg,
BigInteger n)
throws NoSuchAlgorithmException, InvalidKeyException, IOException, BadPaddingException {
return preSignPKCS1v15(msg, n, "SHA-256", AlgorithmId.SHA256_oid, 11, null);
}
public static byte[] preSignPKCS1v15(byte[] msg,
BigInteger n,
String hashFunc,
ObjectIdentifier digestOID,
int reservedBytes,
SecureRandom random)
throws NoSuchAlgorithmException, InvalidKeyException, IOException, BadPaddingException {
if (msg == null) {
throw new IllegalArgumentException("data is null");
}
if (random == null) {
random = JCAUtil.getSecureRandom();
}
MessageDigest md = MessageDigest.getInstance(hashFunc);
int encodedLength = 8 + reservedBytes + md.getDigestLength();
md.update(msg, 0, msg.length);
byte[] digestMsg = md.digest();
RSAPadding padding = null;
try {
padding = RSAPadding.getInstance(1, RSAUtils.getByteLength(n), random);
} catch (InvalidAlgorithmParameterException e) {
throw new InvalidKeyException(e.getMessage());
}
int maxDataSize = padding.getMaxDataSize();
if (encodedLength > maxDataSize) {
throw new InvalidKeyException("Key is too short for this signature algorithm");
}
byte[] encodeSig = RSASignature.encodeSignature(digestOID, digestMsg);
return padding.pad(encodeSig);
}
public static byte[] postSignPKCS1v15(List frgsCiphers, BigInteger n){
BigInteger res = RsaMpcUtils.combineFragmentsDecryptRes(frgsCiphers, n);
int ln = RSAUtils.getByteLength(n);
return RsaMpcUtils.finalRsaResultToByteArray(res, ln);
}
}