public Signature createRawSignature(AlgorithmIdentifier algorithm) { Signature sig; try { String algName = getSignatureName(algorithm); algName = "NONE" + algName.substring(algName.indexOf("WITH")); sig = helper.createSignature(algName); // RFC 4056 // When the id-RSASSA-PSS algorithm identifier is used for a signature, // the AlgorithmIdentifier parameters field MUST contain RSASSA-PSS-params. if (algorithm.getAlgorithm().equals(PKCSObjectIdentifiers.id_RSASSA_PSS)) { AlgorithmParameters params = helper.createAlgorithmParameters(algName); params.init(algorithm.getParameters().toASN1Primitive().getEncoded(), "ASN.1"); PSSParameterSpec spec = (PSSParameterSpec)params.getParameterSpec(PSSParameterSpec.class); sig.setParameter(spec); } } catch (Exception e) { return null; } return sig; }
/** * Return the PKCS#1 ASN.1 structure RSASSA-PSS-params. */ protected byte[] engineGetEncoded() throws IOException { PSSParameterSpec pssSpec = currentSpec; AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier( DigestFactory.getOID(pssSpec.getDigestAlgorithm()), DERNull.INSTANCE); MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)pssSpec.getMGFParameters(); AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier( PKCSObjectIdentifiers.id_mgf1, new AlgorithmIdentifier(DigestFactory.getOID(mgfSpec.getDigestAlgorithm()), DERNull.INSTANCE)); RSASSAPSSparams pssP = new RSASSAPSSparams(hashAlgorithm, maskGenAlgorithm, new ASN1Integer(pssSpec.getSaltLength()), new ASN1Integer(pssSpec.getTrailerField())); return pssP.getEncoded("DER"); }
protected PSSSignatureSpi( AsymmetricBlockCipher signer, PSSParameterSpec baseParamSpec, boolean isRaw) { this.signer = signer; this.originalSpec = baseParamSpec; if (baseParamSpec == null) { this.paramSpec = PSSParameterSpec.DEFAULT; } else { this.paramSpec = baseParamSpec; } this.mgfDigest = DigestFactory.getDigest(paramSpec.getDigestAlgorithm()); this.saltLength = paramSpec.getSaltLength(); this.trailer = getTrailer(paramSpec.getTrailerField()); this.isRaw = isRaw; setupContentDigest(); }
protected AlgorithmParameters engineGetParameters() { if (engineParams == null) { try { engineParams = AlgorithmParameters.getInstance("PSS", BouncyCastleProvider.PROVIDER_NAME); engineParams.init(new PSSParameterSpec(saltLength)); } catch (Exception e) { throw new RuntimeException(e.toString()); } } return engineParams; }
protected AlgorithmParameters engineGetParameters() { if (engineParams == null) { try { engineParams = AlgorithmParameters.getInstance("PSS", "BC"); engineParams.init(new PSSParameterSpec(saltLength)); } catch (Exception e) { throw new RuntimeException(e.toString()); } } return engineParams; }
/** * Return the PKCS#1 ASN.1 structure RSASSA-PSS-params. */ protected byte[] engineGetEncoded() { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); PSSParameterSpec pssSpec = (PSSParameterSpec)currentSpec; RSASSAPSSparams pssP = new RSASSAPSSparams(RSASSAPSSparams.DEFAULT_HASH_ALGORITHM, RSASSAPSSparams.DEFAULT_MASK_GEN_FUNCTION, new DERInteger(pssSpec.getSaltLength()), RSASSAPSSparams.DEFAULT_TRAILER_FIELD); try { dOut.writeObject(pssP); dOut.close(); } catch (IOException e) { throw new RuntimeException("Error encoding PSSParameters"); } return bOut.toByteArray(); }
protected AlgorithmParameterSpec localEngineGetParameterSpec( Class paramSpec) throws InvalidParameterSpecException { if (paramSpec == PSSParameterSpec.class && currentSpec != null) { return currentSpec; } throw new InvalidParameterSpecException("unknown parameter spec passed to PSS parameters object."); }
protected void engineInit( AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (!(paramSpec instanceof PSSParameterSpec)) { throw new InvalidParameterSpecException("PSSParameterSpec required to initialise an PSS algorithm parameters object"); } this.currentSpec = (PSSParameterSpec)paramSpec; }
protected P11RSAPSSSignatureSpi(PSSParameterSpec baseParamSpec, boolean isRaw) { this.originalSpec = baseParamSpec; this.paramSpec = (baseParamSpec == null) ? PSSParameterSpec.DEFAULT : baseParamSpec; this.mgfDigest = DigestFactory.getDigest(paramSpec.getDigestAlgorithm()); this.saltLength = paramSpec.getSaltLength(); this.trailer = getTrailer(paramSpec.getTrailerField()); this.isRaw = isRaw; setupContentDigest(); }
public static byte[] signPSS(PrivateKey privateKey, byte[] signedData) throws SignatureException, InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { Signature signature = Signature.getInstance("SHA256withRSA/PSS", BC); signature.setParameter(new PSSParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), 32, 1)); signature.initSign(privateKey); signature.update(signedData); return signature.sign(); }
public static boolean verifyPSS(PublicKey publicKey, byte[] signedData, byte[] sig) throws SignatureException, InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, InvalidKeySpecException { Signature signature = Signature.getInstance("SHA256withRSA/PSS", BC); signature.setParameter(new PSSParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), 32, 1)); signature.initVerify(publicKey); signature.update(signedData); return signature.verify(sig); }
private static Pair<String, ? extends AlgorithmParameterSpec> getSignatureAlgorithmJcaSignatureAlgorithm(int sigAlgorithm) { switch (sigAlgorithm) { case SIGNATURE_RSA_PSS_WITH_SHA256: return Pair.create( "SHA256withRSA/PSS", new PSSParameterSpec( "SHA-256", "MGF1", MGF1ParameterSpec.SHA256, 256 / 8, 1)); case SIGNATURE_RSA_PSS_WITH_SHA512: return Pair.create( "SHA512withRSA/PSS", new PSSParameterSpec( "SHA-512", "MGF1", MGF1ParameterSpec.SHA512, 512 / 8, 1)); case SIGNATURE_RSA_PKCS1_V1_5_WITH_SHA256: return Pair.create("SHA256withRSA", null); case SIGNATURE_RSA_PKCS1_V1_5_WITH_SHA512: return Pair.create("SHA512withRSA", null); case SIGNATURE_ECDSA_WITH_SHA256: return Pair.create("SHA256withECDSA", null); case SIGNATURE_ECDSA_WITH_SHA512: return Pair.create("SHA512withECDSA", null); case SIGNATURE_DSA_WITH_SHA256: return Pair.create("SHA256withDSA", null); default: throw new IllegalArgumentException( "Unknown signature algorithm: 0x" + Long.toHexString(sigAlgorithm & 0xffffffff)); } }
public RsaPssSha256() { super(AlgorithmIdentifiers.RSA_PSS_USING_SHA256, "SHA256withRSAandMGF1"); MGF1ParameterSpec mgf1pec = MGF1ParameterSpec.SHA256; PSSParameterSpec pssSpec = new PSSParameterSpec(mgf1pec.getDigestAlgorithm(), MGF1, mgf1pec, 32, TRAILER); setAlgorithmParameterSpec(pssSpec); }
public RsaPssSha384() { super(AlgorithmIdentifiers.RSA_PSS_USING_SHA384, "SHA384withRSAandMGF1"); MGF1ParameterSpec mgf1pec = MGF1ParameterSpec.SHA384; PSSParameterSpec pssSpec = new PSSParameterSpec(mgf1pec.getDigestAlgorithm(), MGF1, mgf1pec, 48, TRAILER); setAlgorithmParameterSpec(pssSpec); }
public RsaPssSha512() { super(AlgorithmIdentifiers.RSA_PSS_USING_SHA512, "SHA512withRSAandMGF1"); MGF1ParameterSpec mgf1pec = MGF1ParameterSpec.SHA512; PSSParameterSpec pssSpec = new PSSParameterSpec(mgf1pec.getDigestAlgorithm(), MGF1, mgf1pec, 64, TRAILER); setAlgorithmParameterSpec(pssSpec); }
public Signature createRawSignature(AlgorithmIdentifier algorithm) { Signature sig; try { String algName = getSignatureName(algorithm); algName = "NONE" + algName.substring(algName.indexOf("WITH")); sig = helper.createSignature(algName); // RFC 4056 // When the id-RSASSA-PSS algorithm identifier is used for a signature, // the AlgorithmIdentifier parameters field MUST contain RSASSA-PSS-params. if (algorithm.getAlgorithm().equals(PKCSObjectIdentifiers.id_RSASSA_PSS)) { AlgorithmParameters params = helper.createAlgorithmParameters(algName); AlgorithmParametersUtils.loadParameters(params, algorithm.getParameters()); PSSParameterSpec spec = (PSSParameterSpec)params.getParameterSpec(PSSParameterSpec.class); sig.setParameter(spec); } } catch (Exception e) { return null; } return sig; }
/** * Test #2 for <code>PSSParameterSpec(int)</code> ctor<br> * Assertion: * throws <code>IllegalArgumentException</code> * if <code>saltLen</code> less than 0 */ public final void testPSSParameterSpec0102() { try { new PSSParameterSpec(-1); fail("Expected IAE not thrown"); } catch (IllegalArgumentException e) { } }
/** * Test #1 for <code>PSSParameterSpec(int)</code> ctor<br> * Assertion: constructs using valid parameter * <code>PSSParameterSpec<code> object */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies constructor with valid parameter.", method = "PSSParameterSpec", args = {int.class} ) public final void testPSSParameterSpec0101() { AlgorithmParameterSpec aps = new PSSParameterSpec(20); assertTrue(aps instanceof PSSParameterSpec); }
/** * Test #2 for <code>PSSParameterSpec(int)</code> ctor<br> * Assertion: * throws <code>IllegalArgumentException</code> * if <code>saltLen</code> less than 0 */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies IllegalArgumentException.", method = "PSSParameterSpec", args = {int.class} ) public final void testPSSParameterSpec0102() { try { new PSSParameterSpec(-1); fail("Expected IAE not thrown"); } catch (IllegalArgumentException e) { } }
/** * Test #1 for * <code> * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int) * </code> ctor<br> * Assertion: constructs using valid parameters * <code>PSSParameterSpec<code> object */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies constructor with valid parameters.", method = "PSSParameterSpec", args = {java.lang.String.class, java.lang.String.class, java.security.spec.AlgorithmParameterSpec.class, int.class, int.class} ) public final void testPSSParameterSpec0201() { AlgorithmParameterSpec aps = new PSSParameterSpec("SHA-1", "MGF1", MGF1ParameterSpec.SHA1, 20, 1); assertTrue(aps instanceof PSSParameterSpec); }
/** * Test #2 for * <code> * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int) * </code> ctor<br> * Assertion: * throws <code>NullPointerException</code> * if <code>mdName</code> is null */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies NullPointerException.", method = "PSSParameterSpec", args = {java.lang.String.class, java.lang.String.class, java.security.spec.AlgorithmParameterSpec.class, int.class, int.class} ) public final void testPSSParameterSpec0202() { try { new PSSParameterSpec(null, "MGF1", MGF1ParameterSpec.SHA1, 20, 1); fail("Expected NPE not thrown"); } catch (NullPointerException e) { } }
/** * Test #3 for * <code> * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int) * </code> ctor<br> * Assertion: * throws <code>NullPointerException</code> * if <code>mgfName</code> is null */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies NullPointerException.", method = "PSSParameterSpec", args = {java.lang.String.class, java.lang.String.class, java.security.spec.AlgorithmParameterSpec.class, int.class, int.class} ) public final void testPSSParameterSpec0203() { try { new PSSParameterSpec("SHA-1", null, MGF1ParameterSpec.SHA1, 20, 1); fail("Expected NPE not thrown"); } catch (NullPointerException e) { } }
/** * Test #4 for * <code> * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int) * </code> ctor<br> * Assertion: * throws <code>IllegalArgumentException<code> * if <code>saltLen<code> less than 0 */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies IllegalArgumentException.", method = "PSSParameterSpec", args = {java.lang.String.class, java.lang.String.class, java.security.spec.AlgorithmParameterSpec.class, int.class, int.class} ) public final void testPSSParameterSpec0204() { try { new PSSParameterSpec("SHA-1", "MGF1", MGF1ParameterSpec.SHA1, -20, 1); fail("Expected IAE not thrown"); } catch (IllegalArgumentException e) { } }