Ejemplo n.º 1
0
        private void ParametersTest()
        {
            AllocateRandom(ref _iv, 16);
            AllocateRandom(ref _key, 32);
            AllocateRandom(ref _plnText, 1);

            KeyParams kp = new KeyParams(_key, _iv);

            _cmpText = new byte[1];
            _decText = new byte[1];
            _encText = new byte[1];

            RHX engine = new RHX();

            // 1 byte w/ byte arrays
            {
                CTR          cipher = new CTR(engine, false);
                CipherStream cs     = new CipherStream(cipher);

                cs.Initialize(true, kp);
                cs.Write(_plnText, 0, ref _encText, 0);

                cs.Initialize(false, kp);
                cs.Write(_encText, 0, ref _decText, 0);

                if (!Evaluate.AreEqual(_decText, _plnText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                cipher.Dispose();
            }
            // 1 byte w/ stream
            {
                CTR          cipher = new CTR(engine, false);
                CipherStream cs     = new CipherStream(cipher);
                cs.Initialize(true, kp);
                AllocateRandom(ref _plnText, 1);
                MemoryStream mIn  = new MemoryStream(_plnText);
                MemoryStream mOut = new MemoryStream();
                cs.Write(mIn, mOut);

                cs.Initialize(false, kp);
                MemoryStream mRes = new MemoryStream();
                mOut.Seek(0, SeekOrigin.Begin);
                cs.Write(mOut, mRes);

                if (!Evaluate.AreEqual(mRes.ToArray(), _plnText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                cipher.Dispose();
            }

            // partial block w/ byte arrays
            {
                CTR          cipher = new CTR(engine, false);
                CipherStream cs     = new CipherStream(cipher);
                AllocateRandom(ref _plnText, 15);
                Array.Resize(ref _decText, 15);
                Array.Resize(ref _encText, 15);

                cs.Initialize(true, kp);
                cs.Write(_plnText, 0, ref _encText, 0);

                cs.Initialize(false, kp);
                cs.Write(_encText, 0, ref _decText, 0);

                if (!Evaluate.AreEqual(_decText, _plnText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                cipher.Dispose();
            }
            // partial block w/ stream
            {
                CTR          cipher = new CTR(engine, false);
                CipherStream cs     = new CipherStream(cipher);
                AllocateRandom(ref _plnText, 15);
                Array.Resize(ref _decText, 15);
                Array.Resize(ref _encText, 15);

                cs.Initialize(true, kp);
                MemoryStream mIn  = new MemoryStream(_plnText);
                MemoryStream mOut = new MemoryStream();
                cs.Write(mIn, mOut);

                cs.Initialize(false, kp);
                MemoryStream mRes = new MemoryStream();
                mOut.Seek(0, SeekOrigin.Begin);
                cs.Write(mOut, mRes);

                if (!Evaluate.AreEqual(mRes.ToArray(), _plnText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                cipher.Dispose();
            }

            // random block sizes w/ byte arrays
            {
                for (int i = 0; i < 100; i++)
                {
                    CTR cipher = new CTR(engine, false);

                    int sze = AllocateRandom(ref _plnText);
                    _decText = new byte[sze];
                    _encText = new byte[sze];

                    CipherStream cs = new CipherStream(cipher);
                    cs.Initialize(true, kp);
                    cs.Write(_plnText, 0, ref _encText, 0);

                    cs.Initialize(false, kp);
                    cs.Write(_encText, 0, ref _decText, 0);

                    if (!Evaluate.AreEqual(_decText, _plnText))
                    {
                        throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                    }

                    cipher.Dispose();
                }
            }
            // random block sizes w/ stream
            {
                for (int i = 0; i < 100; i++)
                {
                    CTR cipher = new CTR(engine, false);
                    int sze    = AllocateRandom(ref _plnText);
                    _decText = new byte[sze];
                    _encText = new byte[sze];

                    CipherStream cs = new CipherStream(cipher);
                    cs.Initialize(true, kp);
                    MemoryStream mIn  = new MemoryStream(_plnText);
                    MemoryStream mOut = new MemoryStream();
                    cs.Write(mIn, mOut);

                    cs.Initialize(false, kp);
                    MemoryStream mRes = new MemoryStream();
                    mOut.Seek(0, SeekOrigin.Begin);
                    cs.Write(mOut, mRes);

                    if (!Evaluate.AreEqual(mRes.ToArray(), _plnText))
                    {
                        throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                    }

                    cipher.Dispose();
                }
            }

            engine.Dispose();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Tests the SecureRandom access methods and return ranges
        /// </summary>
        /// <returns>Status</returns>
        public string Run()
        {
            try
            {
                CbcModeTest();
                OnProgress(new TestEventArgs("Passed CBC Mode tests.."));
                CfbModeTest();
                OnProgress(new TestEventArgs("Passed CFB Mode tests.."));
                CtrModeTest();
                OnProgress(new TestEventArgs("Passed CTR Mode tests.."));
                OfbModeTest();
                OnProgress(new TestEventArgs("Passed OFB Mode tests.."));
                StreamTest();
                OnProgress(new TestEventArgs("Passed Stream Cipher tests"));

                SerializeStructTest();
                OnProgress(new TestEventArgs("Passed CipherDescription serialization test.."));
                OnProgress(new TestEventArgs(""));

                OnProgress(new TestEventArgs("***Testing Cipher Parameters***.. "));
                ParametersTest();
                OnProgress(new TestEventArgs("Passed Cipher Parameters test.."));

                RHX eng = new RHX();
                OnProgress(new TestEventArgs("***Testing Padding Modes***.."));
                StreamModesTest(new CBC(eng, false), new X923());
                OnProgress(new TestEventArgs("Passed CBC/X923 CipherStream test.."));
                StreamModesTest(new CBC(eng, false), new PKCS7());
                OnProgress(new TestEventArgs("Passed CBC/PKCS7 CipherStream test.."));
                StreamModesTest(new CBC(eng, false), new TBC());
                OnProgress(new TestEventArgs("Passed CBC/TBC CipherStream test.."));
                StreamModesTest(new CBC(eng, false), new ISO7816());
                OnProgress(new TestEventArgs("Passed CBC/ISO7816 CipherStream test.."));
                OnProgress(new TestEventArgs(""));

                OnProgress(new TestEventArgs("***Testing Cipher Modes***.."));
                StreamModesTest(new CTR(eng, false), new ISO7816());
                OnProgress(new TestEventArgs("Passed CTR CipherStream test.."));
                StreamModesTest(new CFB(eng, 16, false), new ISO7816());
                OnProgress(new TestEventArgs("Passed CFB CipherStream test.."));
                StreamModesTest(new OFB(eng, 16, false), new ISO7816());
                OnProgress(new TestEventArgs("Passed OFB CipherStream test.."));
                OnProgress(new TestEventArgs(""));
                eng.Dispose();

                OnProgress(new TestEventArgs("***Testing Stream Ciphers***.."));
                StreamingTest(new ChaCha20());
                OnProgress(new TestEventArgs("Passed ChaCha CipherStream test.."));
                StreamingTest(new Salsa20());
                OnProgress(new TestEventArgs("Passed Salsa20 CipherStream test.."));
                OnProgress(new TestEventArgs(""));

                OnProgress(new TestEventArgs("***Testing Cipher Description Initialization***.."));
                CipherDescription cd = new CipherDescription(
                    SymmetricEngines.RHX,                               // cipher engine
                    32,                                                 // key size in bytes
                    IVSizes.V128,                                       // cipher iv size
                    CipherModes.CTR,                                    // cipher mode
                    PaddingModes.ISO7816,                               // cipher padding
                    BlockSizes.B128,                                    // cipher block size
                    RoundCounts.R14,                                    // number of transformation rounds
                    Digests.None,                                       // optional key schedule engine (HX ciphers)
                    0,                                                  // optional HMAC size
                    Digests.None);                                      // optional HMAC engine

                DescriptionTest(cd);
                OnProgress(new TestEventArgs("Passed CipherDescription stream test.."));
                OnProgress(new TestEventArgs(""));

                OnProgress(new TestEventArgs("***Testing Block Ciphers***.. "));
                THX tfx = new THX();
                StreamModesTest(new CBC(tfx, false), new ISO7816());
                tfx.Dispose();
                OnProgress(new TestEventArgs("Passed THX CipherStream test.."));
                SHX spx = new SHX();
                StreamModesTest(new CBC(spx, false), new ISO7816());
                spx.Dispose();
                OnProgress(new TestEventArgs("Passed SHX CipherStream test.."));

                Array.Resize(ref _key, 192);
                for (int i = 0; i < 192; i++)
                {
                    _key[i] = (byte)i;
                }

                // test extended ciphers
                RHX rhx = new RHX();
                StreamModesTest(new CBC(rhx, false), new ISO7816());
                rhx.Dispose();
                OnProgress(new TestEventArgs("Passed RHX CipherStream test.."));
                SHX shx = new SHX();
                StreamModesTest(new CBC(shx, false), new ISO7816());
                shx.Dispose();
                OnProgress(new TestEventArgs("Passed SHX CipherStream test.."));
                THX thx = new THX();
                StreamModesTest(new CBC(thx, false), new ISO7816());
                thx.Dispose();
                OnProgress(new TestEventArgs("Passed THX CipherStream test.."));
                OnProgress(new TestEventArgs(""));

                return(SUCCESS);
            }
            catch (Exception Ex)
            {
                string message = Ex.Message == null ? "" : Ex.Message;
                throw new Exception(FAILURE + message);
            }
        }
Ejemplo n.º 3
0
        private void OfbModeTest()
        {
            AllocateRandom(ref _iv, 16);
            AllocateRandom(ref _key, 32);

            KeyParams kp      = new KeyParams(_key, _iv);
            RHX       eng     = new RHX();
            OFB       cipher  = new OFB(eng);
            OFB       cipher2 = new OFB(eng);
            ISO7816   padding = new ISO7816();

            cipher.IsParallel = false;
            CipherStream cs = new CipherStream(cipher2, padding);

            for (int i = 0; i < 10; i++)
            {
                int sze      = AllocateRandom(ref _plnText, 0, eng.BlockSize);
                int prlBlock = sze - (sze % (cipher.BlockSize * _processorCount));
                _cmpText = new byte[sze];
                _decText = new byte[sze];
                _encText = new byte[sze];

                MemoryStream mIn  = new MemoryStream(_plnText);
                MemoryStream mOut = new MemoryStream();
                MemoryStream mRes = new MemoryStream();

                // *** Compare encryption output *** //

                // local processor
                cipher.Initialize(true, kp);
                BlockEncrypt(cipher, padding, _plnText, 0, ref _encText, 0);

                // streamcipher linear mode
                cs.IsParallel = false;
                // memorystream interface
                cs.Initialize(true, kp);
                cs.Write(mIn, mOut);

                if (!Evaluate.AreEqual(mOut.ToArray(), _encText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(true, kp);
                cs.Write(_plnText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _encText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                // ***compare decryption output *** //

                // local processor
                cipher.Initialize(false, kp);
                BlockDecrypt(cipher, padding, _encText, 0, ref _decText, 0);

                if (!Evaluate.AreEqual(_plnText, _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // decrypt linear mode
                cipher2.IsParallel = false;
                mOut.Seek(0, SeekOrigin.Begin);
                cs.Initialize(false, kp);
                cs.Write(mOut, mRes);

                if (!Evaluate.AreEqual(mRes.ToArray(), _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(false, kp);
                Array.Resize(ref _cmpText, _encText.Length);
                cs.Write(_encText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }
            }

            eng.Dispose();
        }