/** * 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"); } }
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); }
/** * 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); }
/** * PSpecified(byte[] p) method testing. Tests that NullPointerException * is thrown in the case of null p array. Also it checks the value of * DEFAULT field, and that input p array is copied to protect against * subsequent modification. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Nested class.", clazz = PSource.PSpecified.class, method = "PSpecified", args = { byte[].class } ) public void testPSpecified() { try { new PSource.PSpecified(null); fail("NullPointerException should be thrown in the case of " + "null p array."); } catch (NullPointerException e) { } assertEquals("The PSource.PSpecified DEFAULT value should be byte[0]", 0, PSource.PSpecified.DEFAULT.getValue().length); byte[] p = new byte[] {1, 2, 3, 4, 5}; PSource.PSpecified ps = new PSource.PSpecified(p); p[0]++; assertFalse("The change of p specified in the constructor " + "should not cause the change of internal array.", p[0] == ps .getValue()[0]); }
/** * getAlgorithm() method testing. Tests that returned value is * equal to the value specified in the constructor. */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getAlgorithm", args = {} ), @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "PSource", args = {java.lang.String.class} ) }) public void testGetAlgorithm() { String pSrcName = "pSrcName"; PSource ps = new PSource(pSrcName) {}; assertTrue("The returned value is not equal to the value specified " + "in constructor", pSrcName.equals(ps.getAlgorithm())); }
/** * PSpecified(byte[] p) method testing. Tests that NullPointerException * is thrown in the case of null p array. Also it checks the value of * DEFAULT field, and that input p array is copied to protect against * subsequent modification. */ public void testPSpecified() { try { new PSource.PSpecified(null); fail("NullPointerException should be thrown in the case of " + "null p array."); } catch (NullPointerException e) { } assertEquals("The PSource.PSpecified DEFAULT value should be byte[0]", 0, PSource.PSpecified.DEFAULT.getValue().length); byte[] p = new byte[] {1, 2, 3, 4, 5}; PSource.PSpecified ps = new PSource.PSpecified(p); p[0] ++; assertFalse("The change of p specified in the constructor " + "should not cause the change of internal array.", p[0] == ps.getValue()[0]); }
private void initFromSpec( OAEPParameterSpec pSpec) throws NoSuchPaddingException { MGF1ParameterSpec mgfParams = (MGF1ParameterSpec)pSpec.getMGFParameters(); Digest digest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm()); if (digest == null) { throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: "+ mgfParams.getDigestAlgorithm()); } cipher = new OAEPEncoding(new RSABlindedEngine(), digest, ((PSource.PSpecified)pSpec.getPSource()).getValue()); paramSpec = pSpec; }
private void initFromSpec( OAEPParameterSpec pSpec) throws NoSuchPaddingException { MGF1ParameterSpec mgfParams = (MGF1ParameterSpec)pSpec.getMGFParameters(); Digest digest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm()); if (digest == null) { throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: "+ mgfParams.getDigestAlgorithm()); } cipher = new BufferedAsymmetricBlockCipher(new OAEPEncoding(new ElGamalEngine(), digest, ((PSource.PSpecified)pSpec.getPSource()).getValue())); paramSpec = pSpec; }
protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (!(paramSpec instanceof OAEPParameterSpec)) { throw new InvalidParameterSpecException ("Inappropriate parameter specification"); } OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec; mdName = spec.getDigestAlgorithm(); String mgfName = spec.getMGFAlgorithm(); if (!mgfName.equalsIgnoreCase("MGF1")) { throw new InvalidParameterSpecException("Unsupported mgf " + mgfName + "; MGF1 only"); } AlgorithmParameterSpec mgfSpec = spec.getMGFParameters(); if (!(mgfSpec instanceof MGF1ParameterSpec)) { throw new InvalidParameterSpecException("Inappropriate mgf " + "parameters; non-null MGF1ParameterSpec only"); } this.mgfSpec = (MGF1ParameterSpec) mgfSpec; PSource pSrc = spec.getPSource(); if (pSrc.getAlgorithm().equals("PSpecified")) { p = ((PSource.PSpecified) pSrc).getValue(); } else { throw new InvalidParameterSpecException("Unsupported pSource " + pSrc.getAlgorithm() + "; PSpecified only"); } }
protected <T extends AlgorithmParameterSpec> T engineGetParameterSpec(Class<T> paramSpec) throws InvalidParameterSpecException { if (OAEPParameterSpec.class.isAssignableFrom(paramSpec)) { return paramSpec.cast( new OAEPParameterSpec(mdName, "MGF1", mgfSpec, new PSource.PSpecified(p))); } else { throw new InvalidParameterSpecException ("Inappropriate parameter specification"); } }
private static boolean comparePSource(OAEPParameterSpec s1, OAEPParameterSpec s2) { PSource src1 = s1.getPSource(); PSource src2 = s2.getPSource(); String alg1 = src1.getAlgorithm(); String alg2 = src2.getAlgorithm(); if (alg1.equals(alg2)) { // assumes they are PSource.PSpecified return Arrays.equals(((PSource.PSpecified) src1).getValue(), ((PSource.PSpecified) src2).getValue()); } else { System.out.println("PSource algos: " + alg1 + " vs " + alg2); return false; } }