private void doTestCorruption() { byte[] kek = Hex.Decode("D1DAA78615F287E6"); byte[] iv = Hex.Decode("EFE598EF21B33D6D"); IWrapper wrapper = new Rfc3211WrapEngine(new DesEngine()); wrapper.Init(false, new ParametersWithIV(new KeyParameter(kek), iv)); byte[] block = Hex.Decode("ff739D838C627C897323A2F8C436F541"); encryptBlock(kek, iv, block); try { wrapper.Unwrap(block, 0, block.Length); Fail("bad length not detected"); } catch (InvalidCipherTextException e) { if (!e.Message.Equals("wrapped key corrupted")) { Fail("wrong exception on length"); } } block = Hex.Decode("08639D838C627C897323A2F8C436F541"); doTestChecksum(kek, iv, block, wrapper); block = Hex.Decode("08736D838C627C897323A2F8C436F541"); doTestChecksum(kek, iv, block, wrapper); block = Hex.Decode("08739D638C627C897323A2F8C436F541"); doTestChecksum(kek, iv, block, wrapper); }
public override void PerformTest() { doWrapTest(1, new DesEngine(), Hex.Decode("D1DAA78615F287E6"), Hex.Decode("EFE598EF21B33D6D"), r1, Hex.Decode("8C627C897323A2F8"), Hex.Decode("B81B2565EE373CA6DEDCA26A178B0C10")); doWrapTest(2, new DesEdeEngine(), Hex.Decode("6A8970BF68C92CAEA84A8DF28510858607126380CC47AB2D"), Hex.Decode("BAF1CA7931213C4E"), r2, Hex.Decode("8C637D887223A2F965B566EB014B0FA5D52300A3F7EA40FFFC577203C71BAF3B"), Hex.Decode("C03C514ABDB9E2C5AAC038572B5E24553876B377AAFB82ECA5A9D73F8AB143D9EC74E6CAD7DB260C")); doTestCorruption(); IWrapper wrapper = new Rfc3211WrapEngine(new DesEngine()); ParametersWithIV parameters = new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16]); byte[] buf = new byte[16]; try { wrapper.Init(true, parameters); wrapper.Unwrap(buf, 0, buf.Length); Fail("failed Unwrap state test."); } catch (InvalidOperationException) { // expected } catch (InvalidCipherTextException e) { Fail("unexpected exception: " + e, e); } try { wrapper.Init(false, parameters); wrapper.Wrap(buf, 0, buf.Length); Fail("failed Unwrap state test."); } catch (InvalidOperationException) { // expected } // // short test // try { wrapper.Init(false, parameters); wrapper.Unwrap(buf, 0, buf.Length / 2); Fail("failed Unwrap short test."); } catch (InvalidCipherTextException) { // expected } }
private void doWrapTest( int id, IBlockCipher engine, byte[] kek, byte[] iv, SecureRandom rand, byte[] inBytes, byte[] outBytes) { IWrapper wrapper = new Rfc3211WrapEngine(engine); wrapper.Init(true, new ParametersWithRandom( new ParametersWithIV(new KeyParameter(kek), iv), rand)); byte[] cText = wrapper.Wrap(inBytes, 0, inBytes.Length); if (!AreEqual(cText, outBytes)) { Fail("failed Wrap test " + id + " expected " + Hex.ToHexString(outBytes) + " got " + Hex.ToHexString(cText)); } wrapper.Init(false, new ParametersWithIV(new KeyParameter(kek), iv)); byte[] pText = wrapper.Unwrap(outBytes, 0, outBytes.Length); if (!AreEqual(pText, inBytes)) { Fail("rfailed Unwrap test " + id + " expected " + Hex.ToHexString(inBytes) + " got " + Hex.ToHexString(pText)); } }
public static IWrapper GetWrapper(string algorithm) { string text = Platform.ToUpperInvariant(algorithm); string text2 = (string)WrapperUtilities.algorithms[text]; if (text2 == null) { text2 = text; } try { switch ((WrapperUtilities.WrapAlgorithm)Enums.GetEnumValue(typeof(WrapperUtilities.WrapAlgorithm), text2)) { case WrapperUtilities.WrapAlgorithm.AESWRAP: { IWrapper result = new AesWrapEngine(); return(result); } case WrapperUtilities.WrapAlgorithm.CAMELLIAWRAP: { IWrapper result = new CamelliaWrapEngine(); return(result); } case WrapperUtilities.WrapAlgorithm.DESEDEWRAP: { IWrapper result = new DesEdeWrapEngine(); return(result); } case WrapperUtilities.WrapAlgorithm.RC2WRAP: { IWrapper result = new RC2WrapEngine(); return(result); } case WrapperUtilities.WrapAlgorithm.SEEDWRAP: { IWrapper result = new SeedWrapEngine(); return(result); } case WrapperUtilities.WrapAlgorithm.DESEDERFC3211WRAP: { IWrapper result = new Rfc3211WrapEngine(new DesEdeEngine()); return(result); } case WrapperUtilities.WrapAlgorithm.AESRFC3211WRAP: { IWrapper result = new Rfc3211WrapEngine(new AesFastEngine()); return(result); } case WrapperUtilities.WrapAlgorithm.CAMELLIARFC3211WRAP: { IWrapper result = new Rfc3211WrapEngine(new CamelliaEngine()); return(result); } } } catch (ArgumentException) { } IBufferedCipher cipher = CipherUtilities.GetCipher(algorithm); if (cipher != null) { return(new WrapperUtilities.BufferedCipherWrapper(cipher)); } throw new SecurityUtilityException("Wrapper " + algorithm + " not recognised."); }