org.bouncycastle.crypto.signers.PSSSigner Java Examples

The following examples show how to use org.bouncycastle.crypto.signers.PSSSigner. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BouncyCastleV1CryptoProvider.java    From paseto with MIT License 6 votes vote down vote up
private PSSSigner pssSha384(boolean forSigning, byte[] key) {
	try {
		byte[] salt = new byte[SHA384_OUT_LEN];
		new SecureRandom().nextBytes(salt);
		// RSA-PSS, SHA-384, MGF1(SHA-384), 48 byte salt length, 0xBC trailer
		PSSSigner pss = new PSSSigner(new RSABlindedEngine(), new SHA384Digest(), new SHA384Digest(),
				SHA384_OUT_LEN, (byte) 0xBC);

		if (forSigning) {
			pss.init(true, PrivateKeyFactory.createKey(key));
		} else {
			pss.init(false, PublicKeyFactory.createKey(key));
		}

		return pss;
	} catch (IOException e) {
		throw new CryptoProviderException("IOException", e);
	}
}
 
Example #2
Source File: BouncyCastleV1CryptoProvider.java    From paseto with MIT License 5 votes vote down vote up
@Override
public byte[] rsaSign(byte[] m, byte[] privateKey) {
	validateRsaSign(m, privateKey);

	try {
		PSSSigner pss = pssSha384(true, privateKey);
		pss.update(m, 0, m.length);
		return pss.generateSignature();
	} catch (CryptoException e) {
		// Not documented
		throw new CryptoProviderException("CryptoException", e);
	}
}
 
Example #3
Source File: BouncyCastleV1CryptoProvider.java    From paseto with MIT License 5 votes vote down vote up
@Override
public boolean rsaVerify(byte[] m, byte[] sig, byte[] publicKey) {
	validateRsaVerify(m, sig, publicKey);

	PSSSigner pss = pssSha384(false, publicKey);
	pss.update(m, 0, m.length);
	return pss.verifySignature(sig);
}
 
Example #4
Source File: PcfAuthentication.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
private static String doSign(byte[] message, String instanceKeyPem) throws CryptoException {

		RSAPrivateKeySpec privateKey = PemObject.fromKey(instanceKeyPem).getRSAKeySpec();
		PSSSigner signer = new PSSSigner(new RSAEngine(), new SHA256Digest(), SALT_LENGTH);

		signer.init(true, new RSAKeyParameters(true, privateKey.getModulus(), privateKey.getPrivateExponent()));
		signer.update(message, 0, message.length);

		byte[] signature = signer.generateSignature();
		return Base64Utils.encodeToUrlSafeString(signature);
	}
 
Example #5
Source File: SignerUtil.java    From xipki with Apache License 2.0 5 votes vote down vote up
public static PSSSigner createPSSRSASigner(AlgorithmIdentifier sigAlgId,
    AsymmetricBlockCipher cipher) throws XiSecurityException {
  Args.notNull(sigAlgId, "sigAlgId");
  if (!PKCSObjectIdentifiers.id_RSASSA_PSS.equals(sigAlgId.getAlgorithm())) {
    throw new XiSecurityException("signature algorithm " + sigAlgId.getAlgorithm()
      + " is not allowed");
  }

  AlgorithmIdentifier digAlgId;
  try {
    digAlgId = AlgorithmUtil.extractDigesetAlgFromSigAlg(sigAlgId);
  } catch (NoSuchAlgorithmException ex) {
    throw new XiSecurityException(ex.getMessage(), ex);
  }

  RSASSAPSSparams param = RSASSAPSSparams.getInstance(sigAlgId.getParameters());

  AlgorithmIdentifier mfgDigAlgId = AlgorithmIdentifier.getInstance(
      param.getMaskGenAlgorithm().getParameters());

  Digest dig = getDigest(digAlgId);
  Digest mfgDig = getDigest(mfgDigAlgId);

  int saltSize = param.getSaltLength().intValue();
  int trailerField = param.getTrailerField().intValue();
  AsymmetricBlockCipher tmpCipher = (cipher == null) ? new RSABlindedEngine() : cipher;

  return new PSSSigner(tmpCipher, dig, mfgDig, saltSize, getTrailer(trailerField));
}
 
Example #6
Source File: SignerUtil.java    From xipki with Apache License 2.0 5 votes vote down vote up
private static byte getTrailer(int trailerField) {
  if (trailerField == 1) {
    return org.bouncycastle.crypto.signers.PSSSigner.TRAILER_IMPLICIT;
  }

  throw new IllegalArgumentException("unknown trailer field");
}
 
Example #7
Source File: SignerUtil.java    From xipki with Apache License 2.0 4 votes vote down vote up
public static PSSSigner createPSSRSASigner(AlgorithmIdentifier sigAlgId)
    throws XiSecurityException {
  return createPSSRSASigner(sigAlgId, null);
}
 
Example #8
Source File: P11ContentSigner.java    From xipki with Apache License 2.0 4 votes vote down vote up
PSSSignerOutputStream(PSSSigner pssSigner) {
  this.pssSigner = pssSigner;
}
 
Example #9
Source File: P11ContentSigner.java    From xipki with Apache License 2.0 4 votes vote down vote up
RSAPSS(P11CryptService cryptService, P11IdentityId identityId,
    AlgorithmIdentifier signatureAlgId, SecureRandom random)
    throws XiSecurityException, P11TokenException {
  super(cryptService, identityId, signatureAlgId);
  Args.notNull(random, "random");

  ASN1ObjectIdentifier sigOid = signatureAlgId.getAlgorithm();
  if (!PKCSObjectIdentifiers.id_RSASSA_PSS.equals(sigOid)) {
    throw new XiSecurityException("unsupported signature algorithm "
        + signatureAlgId.getAlgorithm());
  }

  RSASSAPSSparams asn1Params = RSASSAPSSparams.getInstance(signatureAlgId.getParameters());
  ASN1ObjectIdentifier digestAlgOid = asn1Params.getHashAlgorithm().getAlgorithm();
  HashAlgo hashAlgo = HashAlgo.getInstance(digestAlgOid);
  if (hashAlgo == null) {
    throw new XiSecurityException("unsupported hash algorithm " + digestAlgOid.getId());
  }

  P11SlotIdentifier slotId = identityId.getSlotId();
  P11Slot slot = cryptService.getSlot(slotId);

  long mech = hashAlgMechMap.get(hashAlgo).longValue();
  if (slot.supportsMechanism(mech)) {
    this.mechanism = mech;
    this.parameters = new P11Params.P11RSAPkcsPssParams(asn1Params);
    this.outputStream = new ByteArrayOutputStream();
  } else if (slot.supportsMechanism(PKCS11Constants.CKM_RSA_PKCS_PSS)) {
    this.mechanism = PKCS11Constants.CKM_RSA_PKCS_PSS;
    this.parameters = new P11Params.P11RSAPkcsPssParams(asn1Params);
    this.outputStream = new DigestOutputStream(hashAlgo.createDigest());
  } else if (slot.supportsMechanism(PKCS11Constants.CKM_RSA_X_509)) {
    this.mechanism = PKCS11Constants.CKM_RSA_X_509;
    this.parameters = null;
    AsymmetricBlockCipher cipher = new P11PlainRSASigner();
    P11RSAKeyParameter keyParam;
    try {
      keyParam = P11RSAKeyParameter.getInstance(cryptService, identityId);
    } catch (InvalidKeyException ex) {
      throw new XiSecurityException(ex.getMessage(), ex);
    }
    PSSSigner pssSigner = SignerUtil.createPSSRSASigner(signatureAlgId, cipher);
    pssSigner.init(true, new ParametersWithRandom(keyParam, random));
    this.outputStream = new PSSSignerOutputStream(pssSigner);
  } else {
    throw new XiSecurityException("unsupported signature algorithm "
        + sigOid.getId() + " with " + hashAlgo);
  }
}