/** * Return the PKCS#1 ASN.1 structure RSAES-OAEP-params. */ protected byte[] engineGetEncoded() { AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier( DigestFactory.getOID(currentSpec.getDigestAlgorithm()), DERNull.INSTANCE); MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)currentSpec.getMGFParameters(); AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier( PKCSObjectIdentifiers.id_mgf1, new AlgorithmIdentifier(DigestFactory.getOID(mgfSpec.getDigestAlgorithm()), DERNull.INSTANCE)); PSource.PSpecified pSource = (PSource.PSpecified)currentSpec.getPSource(); AlgorithmIdentifier pSourceAlgorithm = new AlgorithmIdentifier( PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(pSource.getValue())); RSAESOAEPparams oaepP = new RSAESOAEPparams(hashAlgorithm, maskGenAlgorithm, pSourceAlgorithm); try { return oaepP.getEncoded(ASN1Encoding.DER); } catch (IOException e) { throw new RuntimeException("Error encoding OAEPParameters"); } }
/** * 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"); }
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec, byte[] p) throws Exception { OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1", mgfSpec, new PSource.PSpecified(p)); cp = Security.getProvider("SunJCE"); System.out.println("Testing provider " + cp.getName() + "..."); AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp); ap.init(spec); byte[] encoding = ap.getEncoded(); AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp); ap2.init(encoding); OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec (OAEPParameterSpec.class); return compareSpec(spec, spec2); }
private static boolean compareMGF(OAEPParameterSpec s1, OAEPParameterSpec s2) { String alg1 = s1.getMGFAlgorithm(); String alg2 = s2.getMGFAlgorithm(); if (alg1.equals(alg2)) { MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters(); MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters(); alg1 = mp1.getDigestAlgorithm(); alg2 = mp2.getDigestAlgorithm(); if (alg1.equals(alg2)) { return true; } else { System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2); return false; } } else { System.out.println("MGF algos: " + alg1 + " vs " + alg2); return false; } }
public static void main(String[] argv) throws Exception { boolean status = true; byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 }; status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p); status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p); status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p); status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p); status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]); status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]); status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]); if (status) { System.out.println("Test Passed"); } else { throw new Exception("One or More Test Failed"); } }
/** * getDigestAlgorithm() method testing. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getDigestAlgorithm", args = {} ) public void testGetDigestAlgorithm() { String mdName = "SHA-1"; String mgfName = "MGF1"; AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1; PSource pSrc = PSource.PSpecified.DEFAULT; OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName, mgfSpec, pSrc); assertTrue("The returned value does not equal to the " + "value specified in the constructor.", ps.getDigestAlgorithm().equals(mdName)); }
/** * getMGFAlgorithm() method testing. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getMGFAlgorithm", args = {} ) public void testGetMGFAlgorithm() { String mdName = "SHA-1"; String mgfName = "MGF1"; AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1; PSource pSrc = PSource.PSpecified.DEFAULT; OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName, mgfSpec, pSrc); assertTrue("The returned value does not equal to the " + "value specified in the constructor.", ps.getMGFAlgorithm().equals(mgfName)); }
/** * getMGFParameters() method testing. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getMGFParameters", args = {} ) public void testGetMGFParameters() { String mdName = "SHA-1"; String mgfName = "MGF1"; AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1; PSource pSrc = PSource.PSpecified.DEFAULT; OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName, mgfSpec, pSrc); assertTrue("The returned value does not equal to the " + "value specified in the constructor.", ps.getMGFParameters() == mgfSpec); }
/** * getPSource() method testing. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getPSource", args = {} ) public void testGetPSource() { String mdName = "SHA-1"; String mgfName = "MGF1"; AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1; PSource pSrc = PSource.PSpecified.DEFAULT; OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName, mgfSpec, pSrc); assertTrue("The returned value does not equal to the " + "value specified in the constructor.", ps.getPSource() == pSrc); }
/** * Test #1 for <code>MGF1ParameterSpec</code> constructor<br> * Assertion: constructs new <code>MGF1ParameterSpec</code> * object using valid parameter */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "MGF1ParameterSpec", args = {java.lang.String.class} ) public final void testMGF1ParameterSpec01() { try { MGF1ParameterSpec pgf = new MGF1ParameterSpec(testAlgName); assertNotNull(pgf); assertTrue(pgf instanceof MGF1ParameterSpec); } catch (Exception e) { fail("Unexpected exception: " + e); } }