Ejemplo n.º 1
43
        private void ParallelTest()
        {
            CSPRng rng = new CSPRng();
            byte[] key = rng.GetBytes(32);
            byte[] iv = rng.GetBytes(8);
            byte[] data = rng.GetBytes(2048);
            byte[] enc = new byte[2048];
            byte[] dec = new byte[2048];
            rng.Dispose();

            using (ChaCha chacha = new ChaCha(10))
            {
                // encrypt linear
                chacha.Initialize(new KeyParams(key, iv));
                chacha.IsParallel = false;
                chacha.Transform(data, enc);
                // decrypt parallel
                chacha.Initialize(new KeyParams(key, iv));
                chacha.IsParallel = true;
                chacha.ParallelBlockSize = 2048;
                chacha.Transform(enc, dec);
            }

            if (!Compare.AreEqual(data, dec))
                throw new Exception("ChaCha: Decrypted arrays are not equal!");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Tests the SecureRandom access methods and return ranges
        /// </summary>
        /// <returns>Status</returns>
        public string Test()
        {
            try
            {
                SecRandTest();

                RandRangeTest(new BBSG(), 10);
                OnProgress(new TestEventArgs("Passed BBSG threshhold tests.."));
                RandRangeTest(new CCG(), 10);
                OnProgress(new TestEventArgs("Passed CCG threshhold tests.."));
                RandRangeTest(new CSPRng());
                OnProgress(new TestEventArgs("Passed CSPRng threshhold tests.."));
                RandRangeTest(new CTRPrng());
                OnProgress(new TestEventArgs("Passed CTRPrng threshhold tests.."));
                RandRangeTest(new DGCPrng());
                OnProgress(new TestEventArgs("Passed DGCPrng threshhold tests.."));
                RandRangeTest(new MODEXPG(), 10);
                OnProgress(new TestEventArgs("Passed MODEXPG threshhold tests.."));
                RandRangeTest(new QCG1(), 10);
                OnProgress(new TestEventArgs("Passed QCG1 threshhold tests.."));
                RandRangeTest(new QCG2(), 10);
                OnProgress(new TestEventArgs("Passed QCG2 threshhold tests.."));
                RandRangeTest(new SP20Prng(), 10);
                OnProgress(new TestEventArgs("Passed SP20Prng threshhold tests.."));

                byte[] pass, salt;
                CSPRng rand = new CSPRng();
                pass = rand.GetBytes(23);
                salt = rand.GetBytes(256);
                rand.Dispose();
                RandRangeTest(new PBPRng(new SHA512(), pass, salt));
                OnProgress(new TestEventArgs("Passed PBPRng threshhold tests.."));
                return SUCCESS;
            }
            catch (Exception Ex)
            {
                string message = Ex.Message == null ? "" : Ex.Message;
                throw new Exception(FAILURE + message);
            }
        }
Ejemplo n.º 3
0
        /// <remarks>
        /// 4 stage overwrite: random, reverse random, ones, zeros. 
        /// Last overwrite stage is zeros in Extract() method.
        /// </remarks>
        private void Erase(long Offset, long Length)
        {
            byte[] buffer =  new byte[Length];

            // get p-rand buffer
            using (CSPRng csp = new CSPRng())
                csp.GetBytes(buffer);

            // rand
            Overwrite(buffer, Offset, Length);
            // reverse rand
            Array.Reverse(buffer);
            Overwrite(buffer, Offset, Length);
            // ones
            for (int i = 0; i < buffer.Length; i++)
                buffer[i] = (byte)255;
            Overwrite(buffer, Offset, Length);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// A PackageKey header structure. 
        /// </summary>
        /// 
        /// <param name="Authority">The <see cref="KeyAuthority">KeyAuthority</see> structure containing the key authorization schema.</param>
        /// <param name="Cipher">The <see cref="CipherDescription">CipherDescription</see> structure containing a complete description of the cipher instance.</param>
        /// <param name="SubKeyCount">The number of Key Sets contained in this key package file.</param>
        /// <param name="ExtensionKey">An array of random bytes used to encrypt a message file extension. A null value auto generates this field.</param>
        /// 
        /// <exception cref="CryptoProcessingException">Thrown if an invalid ExtensionKey is used</exception>
        public PackageKey(KeyAuthority Authority, CipherDescription Cipher, int SubKeyCount, byte[] ExtensionKey = null)
        {
            this.KeyPolicy = Authority.KeyPolicy;
            this.Authority = Authority;
            this.Description = Cipher;
            this.SubKeyCount = SubKeyCount;
            SubKeyPolicy = new long[SubKeyCount];
            SubKeyID = new byte[SubKeyCount][];

            // generate the subkey ids and set master policy
            for (int i = 0; i < SubKeyCount; i++)
            {
                SubKeyPolicy[i] = (long)Authority.KeyPolicy;
                SubKeyID[i] = Guid.NewGuid().ToByteArray();
            }

            if (ExtensionKey != null)
            {
                if (ExtensionKey.Length != 16)
                    throw new CryptoProcessingException("PackageKey:CTor", "Extension key must be exactly 16 bytes in length!", new ArgumentOutOfRangeException());

                this.ExtensionKey = ExtensionKey;
            }
            else
            {
                using (CSPRng rand = new CSPRng())
                    this.ExtensionKey = rand.GetBytes(16);
            }

            CreatedOn = DateTime.Now.Ticks;
        }
Ejemplo n.º 5
0
        private void DigestTest()
        {
            CSPRng rng = new CSPRng();
            NTRUParameters param = (NTRUParameters)NTRUParamSets.EES1087EP2.DeepCopy();

            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                    param.Digest = Digests.Blake512;//blake512
                else if (i == 1)
                    param.Digest = Digests.Keccak512;//keccak512
                else
                    param.Digest = Digests.Skein512;//skein512

                NTRUKeyPair kp;
                using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param))
                    kp = (NTRUKeyPair)kg.GenerateKeyPair();

                using (NTRUEncrypt ntru = new NTRUEncrypt(param))
                {
                    byte[] plainText = rng.GetBytes(32);
                    ntru.Initialize(kp.PublicKey);
                    byte[] encrypted = ntru.Encrypt(plainText);
                    ntru.Initialize(kp);
                    byte[] decrypted = ntru.Decrypt(encrypted);

                    if (!Compare.AreEqual(plainText, decrypted))
                        throw new Exception("NtruEncryptTest: digest test failed!");
                }
            }
        }