Ejemplo n.º 1
0
        /// <summary>
        /// Uses the cipher to generate a BCrypt hash.
        /// </summary>
        /// <returns>A BCrypt hash.</returns>
        public byte[] BCrypt()
        {
            uint[] magicWords = null;
            byte[] magicBytes = null;

            try
            {
                magicWords = (uint[])Magic.Clone();
                for (int j = 0; j < magicWords.Length; j += 2)
                {
                    for (int i = 0; i < 64; i++)
                    {
                        Encipher(ref magicWords[j], ref magicWords[j + 1]);
                    }
                }

                magicBytes = new byte[magicWords.Length * 4];
                for (int i = 0; i < magicWords.Length; i++)
                {
                    BitPacking.BEBytesFromUInt32(magicWords[i], magicBytes, i * 4);
                }
                return(ByteArray.TruncateAndCopy(magicBytes, magicBytes.Length - 1));
            }
            finally
            {
                Security.Clear(magicWords);
                Security.Clear(magicBytes);
            }
        }
Ejemplo n.º 2
0
        private void decodePage(int[] @in, IntWrapper inpos, int[] @out, IntWrapper outpos, int thissize)
        {
            int tmpoutpos = outpos.get();
            int tmpinpos  = inpos.get();

            for (int run = 0; run < thissize / BLOCK_SIZE; ++run, tmpoutpos += BLOCK_SIZE)
            {
                int b          = @in[tmpinpos] & 0xFF;
                int cexcept    = (int)((uint)@in[tmpinpos] >> 8) & 0xFF;
                int exceptsize = (int)((uint)@in[tmpinpos] >> 16);
                ++tmpinpos;
                S9.uncompress(@in, tmpinpos, exceptsize, exceptbuffer,
                              0, 2 * cexcept);
                tmpinpos += exceptsize;
                for (int k = 0; k < BLOCK_SIZE; k += 32)
                {
                    BitPacking.fastunpack(@in, tmpinpos, @out,
                                          tmpoutpos + k, bits[b]);
                    tmpinpos += bits[b];
                }
                for (int k = 0; k < cexcept; ++k)
                {
                    @out[tmpoutpos + exceptbuffer[k + cexcept]] |= (exceptbuffer[k] << bits[b]);
                }
            }
            outpos.set(tmpoutpos);
            inpos.set(tmpinpos);
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public override string GenerateSalt(CrypterOptions options)
        {
            Check.Null("options", options);

            int?rounds = options.GetValue <int?>(CrypterOption.Rounds);

            if (rounds != null)
            {
                Check.Range("CrypterOption.Rounds", (int)rounds, MinRounds, MaxRounds);
            }

            byte[] roundsBytes = new byte[3], saltBytes = null;
            try
            {
                BitPacking.LEBytesFromUInt24((uint)(rounds ?? 4321), roundsBytes, 0);
                saltBytes = Security.GenerateRandomBytes(3);

                return("_"
                       + Base64Encoding.UnixMD5.GetString(roundsBytes)
                       + Base64Encoding.UnixMD5.GetString(saltBytes));
            }
            finally
            {
                Security.Clear(roundsBytes);
                Security.Clear(saltBytes);
            }
        }
Ejemplo n.º 4
0
        private void DesBegin(byte[] inputBuffer, int inputOffset, out uint L, out uint R)
        {
            ulong p  = BitPacking.UInt64FromBEBytes(inputBuffer, inputOffset);
            ulong pp = Permute(IP, p, 64); p = 0;

            L = (uint)(pp >> 32);  R = (uint)pp;
        }
Ejemplo n.º 5
0
        void DesEnd(byte[] outputBuffer, int outputOffset, ref uint L, ref uint R)
        {
            ulong rl  = (ulong)R << 32 | L; L = 0; R = 0;
            ulong rlp = Permute(FP, rl, 64); rl = 0;

            BitPacking.BEBytesFromUInt64(rlp, outputBuffer, outputOffset);
        }
Ejemplo n.º 6
0
        public void verifyWithExceptions()
        {
            const int N     = 32;
            const int TIMES = 1000;
            Random    r     = new Random();

            int[] data         = new int[N];
            int[] compressed   = new int[N];
            int[] uncompressed = new int[N];
            for (int bit = 0; bit < 31; ++bit)
            {
                for (int t = 0; t < TIMES; ++t)
                {
                    for (int k = 0; k < N; ++k)
                    {
                        data[k] = r.Next();
                    }
                    BitPacking.fastpack(data, 0, compressed, 0, bit);
                    BitPacking.fastunpack(compressed, 0, uncompressed, 0, bit);

                    // Check assertions.
                    maskArray(data, ((1 << bit) - 1));
                    Assert2.assertArrayEquals(data, uncompressed);
                }
            }
        }
Ejemplo n.º 7
0
        static byte[] MFcrypt(byte[] P, byte[] S,
                              int cost, int blockSize, int parallel, int?maxThreads)
        {
            int MFLen = blockSize * 128;

            if (maxThreads == null)
            {
                maxThreads = int.MaxValue;
            }

            if (!BitMath.IsPositivePowerOf2(cost))
            {
                throw Exceptions.ArgumentOutOfRange("cost", "Cost must be a positive power of 2.");
            }
            Check.Range("blockSize", blockSize, 1, int.MaxValue / 128);
            Check.Range("parallel", parallel, 1, int.MaxValue / MFLen);
            Check.Range("maxThreads", (int)maxThreads, 1, int.MaxValue);

            byte[] B = Pbkdf2.ComputeDerivedKey(new HMACSHA256(P), S, 1, parallel * MFLen);

            uint[] B0 = new uint[B.Length / 4];
            for (int i = 0; i < B0.Length; i++)
            {
                B0[i] = BitPacking.UInt32FromLEBytes(B, i * 4);
            }                                                                                       // code is easier with uint[]
            ThreadSMixCalls(B0, MFLen, cost, blockSize, parallel, (int)maxThreads);
            for (int i = 0; i < B0.Length; i++)
            {
                BitPacking.LEBytesFromUInt32(B0[i], B, i * 4);
            }
            Security.Clear(B0);

            return(B);
        }
Ejemplo n.º 8
0
        void ExpandKey(byte[] key, byte[] salt, EksBlowfishKeyExpansionFlags flags)
        {
            uint[] p = P; uint[][] s = S;
            int    i, j, k; uint data, datal, datar;

            j = 0;
            for (i = 0; i < p.Length; i++)
            {
                data = 0x00000000;
                for (k = 0; k < 4; k++)
                {
                    if ((flags & EksBlowfishKeyExpansionFlags.EmulateCryptBlowfishSignExtensionBug) != 0)
                    {
                        data = (data << 8) | (uint)(int)(sbyte)key[j];
                    }
                    else
                    {
                        data = (data << 8) | key[j];
                    }

                    j = (j + 1) % key.Length;
                }
                p[i] = p[i] ^ data;
            }

            uint saltL0 = BitPacking.UInt32FromBEBytes(salt, 0);
            uint saltR0 = BitPacking.UInt32FromBEBytes(salt, 4);
            uint saltL1 = BitPacking.UInt32FromBEBytes(salt, 8);
            uint saltR1 = BitPacking.UInt32FromBEBytes(salt, 12);

            datal = 0x00000000;
            datar = 0x00000000;

            for (i = 0; i < p.Length; i += 4)
            {
                datal ^= saltL0; datar ^= saltR0;
                Encipher(ref datal, ref datar); p[i + 0] = datal; p[i + 1] = datar;

                if (i + 2 == p.Length)
                {
                    break;
                }                                                 // 18 here
                datal ^= saltL1; datar ^= saltR1;
                Encipher(ref datal, ref datar); p[i + 2] = datal; p[i + 3] = datar;
            }

            for (i = 0; i < s.Length; i++)
            {
                uint[] sb = s[i];
                for (j = 0; j < sb.Length; j += 4)
                {
                    datal ^= saltL1; datar ^= saltR1;
                    Encipher(ref datal, ref datar); sb[j + 0] = datal; sb[j + 1] = datar;

                    datal ^= saltL0; datar ^= saltR0;
                    Encipher(ref datal, ref datar); sb[j + 2] = datal; sb[j + 3] = datar;
                }
            }
        }
Ejemplo n.º 9
0
        /// <inheritdoc />
        public override string Crypt(byte[] password, string salt)
        {
            Check.Null("password", password);
            Check.Null("salt", salt);

            Match match = _regex.Match(salt);

            if (!match.Success)
            {
                throw Exceptions.Argument("salt", "Invalid salt.");
            }

            byte[] roundsBytes = null, saltBytes = null, crypt = null, input = null;
            try
            {
                string roundsString = match.Groups["rounds"].Value;
                roundsBytes = Base64Encoding.UnixMD5.GetBytes(roundsString);
                int roundsValue = (int)BitPacking.UInt24FromLEBytes(roundsBytes, 0);

                string saltString = match.Groups["salt"].Value;
                saltBytes = Base64Encoding.UnixMD5.GetBytes(saltString);
                int saltValue = (int)BitPacking.UInt24FromLEBytes(saltBytes, 0);

                input = new byte[8];
                int length = ByteArray.NullTerminatedLength(password, password.Length);

                for (int m = 0; m < length; m += 8)
                {
                    if (m != 0)
                    {
                        using (DesCipher cipher = DesCipher.Create(input))
                        {
                            cipher.Encipher(input, 0, input, 0);
                        }
                    }

                    for (int n = 0; n < 8 && n < length - m; n++)
                    {
                        // DES Crypt ignores the high bit of every byte.
                        input[n] ^= (byte)(password[m + n] << 1);
                    }
                }

                using (DesCipher cipher = DesCipher.Create(input))
                {
                    crypt = new byte[8];
                    cipher.Crypt(crypt, 0, roundsValue, saltValue);
                }

                return("_" + roundsString + saltString + Base64Encoding.UnixCrypt.GetString(crypt));
            }
            finally
            {
                Security.Clear(roundsBytes);
                Security.Clear(saltBytes);
                Security.Clear(crypt);
                Security.Clear(input);
            }
        }
Ejemplo n.º 10
0
 private static int xorUnpack(int[] inBuf, int inOff, int[] outBuf, int outOff, int validBits, int context, int[] work)
 {
     BitPacking.fastunpack(inBuf, inOff, work, 0, validBits);
     outBuf[outOff] = context = work[0] ^ context;
     for (int i = 1, p = outOff + 1; i < 32; ++i, ++p)
     {
         outBuf[p] = context = work[i] ^ context;
     }
     return(validBits);
 }
Ejemplo n.º 11
0
        static BlowfishCipher()
        {
            byte[] magicBytes = Encoding.ASCII.GetBytes(BCryptMagic);
            Array.Resize(ref magicBytes, (magicBytes.Length + 7) / 8 * 8);

            Magic = new uint[(magicBytes.Length + 3) / 4];
            for (int i = 0; i < Magic.Length; i++)
            {
                Magic[i] = BitPacking.UInt32FromBEBytes(magicBytes, i * 4);
            }
        }
Ejemplo n.º 12
0
        private static int xorPack(int[] inBuf, int inOff, int[] outBuf, int outOff, int validBits, int context, int[] work)
        {
            work[0] = inBuf[inOff] ^ context;
            for (int i = 1, p = inOff + 1; i < 32; ++i, ++p)
            {
                work[i] = inBuf[p] ^ inBuf[p - 1];
            }
            BitPacking.fastpackwithoutmask(work, 0, outBuf, outOff,
                                           validBits);

            return(validBits);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Reverses the encipherment of eight bytes of data from one buffer and places the result in another buffer.
        /// </summary>
        /// <param name="inputBuffer">The buffer to read enciphered data from.</param>
        /// <param name="inputOffset">The offset of the first enciphered byte.</param>
        /// <param name="outputBuffer">The buffer to write plaintext data to.</param>
        /// <param name="outputOffset">The offset at which to place the first plaintext byte.</param>
        public void Decipher
            (byte[] inputBuffer, int inputOffset,
            byte[] outputBuffer, int outputOffset)
        {
            CheckCipherBuffers(inputBuffer, inputOffset, outputBuffer, outputOffset);

            uint xl = BitPacking.UInt32FromBEBytes(inputBuffer, inputOffset + 0);
            uint xr = BitPacking.UInt32FromBEBytes(inputBuffer, inputOffset + 4);

            Decipher(ref xl, ref xr);
            BitPacking.BEBytesFromUInt32(xl, outputBuffer, outputOffset + 0);
            BitPacking.BEBytesFromUInt32(xr, outputBuffer, outputOffset + 4);
        }
Ejemplo n.º 14
0
        static byte[] MFcrypt(byte[] P, byte[] S,
                              int cost, int blockSize, int parallel, int?maxThreads)
        {
            int MFLen = blockSize * 128;

            if (maxThreads == null)
            {
                maxThreads = int.MaxValue;
            }

            if (!BitMath.IsPositivePowerOf2(cost))
            {
                throw Exceptions.ArgumentOutOfRange("cost", "Cost must be a positive power of 2.");
            }
            Check.Range("blockSize", blockSize, 1, int.MaxValue / 128);
            Check.Range("parallel", parallel, 1, int.MaxValue / MFLen);
            Check.Range("maxThreads", (int)maxThreads, 1, int.MaxValue);

#if NO_NATIVE_HMACSHA512
            var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new NBitcoin.BouncyCastle.Crypto.Digests.Sha256Digest());
            mac.Init(new KeyParameter(P));
            byte[] B = Pbkdf2.ComputeDerivedKey(mac, S, 1, parallel * MFLen);
#elif NO_NATIVE_RFC2898_HMACSHA512
            byte[] B = Pbkdf2.ComputeDerivedKey(new HMACSHA256(P), S, 1, parallel * MFLen);
#else
            byte[] B = null;
            if (S.Length >= 8)
            {
                // While we should be able to use Rfc2898DeriveBytes if salt is less than 8 bytes, it sadly does not accept salt less than 8 bytes needed for BIP38
                using System.Security.Cryptography.Rfc2898DeriveBytes derive = new System.Security.Cryptography.Rfc2898DeriveBytes(P, S, 1, System.Security.Cryptography.HashAlgorithmName.SHA256);
                B = derive.GetBytes(parallel * MFLen);
            }
            else
            {
                B = Pbkdf2.ComputeDerivedKey(new HMACSHA256(P), S, 1, parallel * MFLen);
            }
#endif
            uint[] B0 = new uint[B.Length / 4];
            for (int i = 0; i < B0.Length; i++)
            {
                B0[i] = BitPacking.UInt32FromLEBytes(B, i * 4);
            }             // code is easier with uint[]
            ThreadSMixCalls(B0, MFLen, cost, blockSize, parallel, (int)maxThreads);
            for (int i = 0; i < B0.Length; i++)
            {
                BitPacking.LEBytesFromUInt32(B0[i], B, i * 4);
            }
            Security.Clear(B0);

            return(B);
        }
        public static void test(bool verbose)
        {
            const int N     = 32;
            const int times = 100000;
            Random    r     = new Random(0);

            int[] data         = new int[N];
            int[] compressed   = new int[N];
            int[] uncompressed = new int[N];

            for (int bit = 0; bit < 31; ++bit)
            {
                long comp   = 0;
                long compwm = 0;
                long decomp = 0;
                for (int t = 0; t < times; ++t)
                {
                    for (int k = 0; k < N; ++k)
                    {
                        data[k] = r.Next(1 << bit);
                    }

                    long time1 = Port.System.nanoTime();
                    BitPacking
                    .fastpack(data, 0, compressed, 0, bit);
                    long time2 = Port.System.nanoTime();
                    BitPacking.fastpackwithoutmask(data, 0,
                                                   compressed, 0, bit);
                    long time3 = Port.System.nanoTime();
                    BitPacking.fastunpack(compressed, 0,
                                          uncompressed, 0, bit);
                    long time4 = Port.System.nanoTime();
                    comp   += time2 - time1;
                    compwm += time3 - time2;
                    decomp += time4 - time3;
                }
                if (verbose)
                {
                    Console.WriteLine("bit = "
                                      + bit
                                      + " comp. speed = "
                                      + (N * times * 1000.0 / (comp)).ToString("0")
                                      + " comp. speed wm = "
                                      + (N * times * 1000.0 / (compwm)).ToString("0")
                                      + " decomp. speed = "
                                      + (N * times * 1000.0 / (decomp)).ToString("0"));
                }
            }
        }
Ejemplo n.º 16
0
        private void ComputeBlock(uint pos)
        {
            BitPacking.BEBytesFromUInt32(pos, this._saltBuffer, this._saltBuffer.Length - 4);
            ComputeHmac(this._saltBuffer, this._digestT1);
            Array.Copy(this._digestT1, this._digest, this._digestT1.Length);

            for (int i = 1; i < this._iterations; i++)
            {
                ComputeHmac(this._digestT1, this._digestT1);
                for (int j = 0; j < this._digest.Length; j++)
                {
                    this._digest[j] ^= this._digestT1[j];
                }
            }

            Security.Clear(this._digestT1);
        }
Ejemplo n.º 17
0
        void ComputeBlock(uint pos)
        {
            BitPacking.BEBytesFromUInt32(pos, _saltBuffer, _saltBuffer.Length - 4);
            ComputeHmac(_saltBuffer, _digestT1);
            Array.Copy(_digestT1, _digest, _digestT1.Length);

            for (int i = 1; i < _iterations; i++)
            {
                ComputeHmac(_digestT1, _digestT1);
                for (int j = 0; j < _digest.Length; j++)
                {
                    _digest[j] ^= _digestT1[j];
                }
            }

            NBitcoin.Crypto.Internal.Security.Clear(_digestT1);
        }
Ejemplo n.º 18
0
        private void encodePage(int[] @in, IntWrapper inpos, int thissize,
                                int[] @out, IntWrapper outpos)
        {
            int        tmpoutpos  = outpos.get();
            int        tmpinpos   = inpos.get();
            IntWrapper bestb      = new IntWrapper();
            IntWrapper bestexcept = new IntWrapper();

            for (int finalinpos = tmpinpos + thissize; tmpinpos + BLOCK_SIZE <= finalinpos; tmpinpos += BLOCK_SIZE)
            {
                getBestBFromData(@in, tmpinpos, bestb, bestexcept);
                int tmpbestb   = bestb.get();
                int nbrexcept  = bestexcept.get();
                int exceptsize = 0;
                int remember   = tmpoutpos;
                tmpoutpos++;
                if (nbrexcept > 0)
                {
                    int c = 0;
                    for (int i = 0; i < BLOCK_SIZE; ++i)
                    {
                        if ((int)((uint)@in[tmpinpos + i] >> bits[tmpbestb]) != 0)
                        {
                            exceptbuffer[c + nbrexcept] = i;
                            exceptbuffer[c]             = (int)((uint)@in[tmpinpos + i] >> bits[tmpbestb]);
                            ++c;
                        }
                    }
                    exceptsize = S9.compress(exceptbuffer, 0,
                                             2 * nbrexcept, @out, tmpoutpos);
                    tmpoutpos += exceptsize;
                }
                @out[remember] = tmpbestb | (nbrexcept << 8)
                                 | (exceptsize << 16);
                for (int k = 0; k < BLOCK_SIZE; k += 32)
                {
                    BitPacking.fastpack(@in, tmpinpos + k, @out,
                                        tmpoutpos, bits[tmpbestb]);
                    tmpoutpos += bits[tmpbestb];
                }
            }
            inpos.set(tmpinpos);
            outpos.set(tmpoutpos);
        }
Ejemplo n.º 19
0
        public void headlessCompress(int[] @in, IntWrapper inpos, int inlength, int[] @out, IntWrapper outpos)
        {
            inlength = Util.greatestMultiple(inlength, BLOCK_SIZE);
            int tmpoutpos = outpos.get();
            int s         = inpos.get();

            for (; s + BLOCK_SIZE * 4 - 1 < inpos.get() + inlength; s += BLOCK_SIZE * 4)
            {
                int mbits1 = Util.maxbits(@in, s, BLOCK_SIZE);

                int mbits2 = Util.maxbits(@in, s + BLOCK_SIZE, BLOCK_SIZE);

                int mbits3 = Util.maxbits(@in, s + 2 * BLOCK_SIZE, BLOCK_SIZE);

                int mbits4 = Util.maxbits(@in, s + 3 * BLOCK_SIZE, BLOCK_SIZE);
                @out[tmpoutpos++] = (mbits1 << 24) | (mbits2 << 16)
                                    | (mbits3 << 8) | (mbits4);
                BitPacking.fastpackwithoutmask(@in, s, @out, tmpoutpos,
                                               mbits1);
                tmpoutpos += mbits1;
                BitPacking.fastpackwithoutmask(@in, s + BLOCK_SIZE, @out,
                                               tmpoutpos, mbits2);
                tmpoutpos += mbits2;
                BitPacking.fastpackwithoutmask(@in, s + 2 * BLOCK_SIZE, @out,
                                               tmpoutpos, mbits3);
                tmpoutpos += mbits3;
                BitPacking.fastpackwithoutmask(@in, s + 3 * BLOCK_SIZE, @out,
                                               tmpoutpos, mbits4);
                tmpoutpos += mbits4;
            }
            for (; s < inpos.get() + inlength; s += BLOCK_SIZE)
            {
                int mbits = Util.maxbits(@in, s, BLOCK_SIZE);
                @out[tmpoutpos++] = mbits;
                BitPacking.fastpackwithoutmask(@in, s, @out, tmpoutpos,
                                               mbits);
                tmpoutpos += mbits;
            }
            inpos.add(inlength);
            outpos.set(tmpoutpos);
        }
Ejemplo n.º 20
0
        static byte[] MFcrypt(byte[] P, byte[] S,
                              int cost, int blockSize, int parallel, int?maxThreads)
        {
            int MFLen = blockSize * 128;

            if (maxThreads == null)
            {
                maxThreads = int.MaxValue;
            }

            if (!BitMath.IsPositivePowerOf2(cost))
            {
                throw Exceptions.ArgumentOutOfRange("cost", "Cost must be a positive power of 2.");
            }
            Check.Range("blockSize", blockSize, 1, int.MaxValue / 128);
            Check.Range("parallel", parallel, 1, int.MaxValue / MFLen);
            Check.Range("maxThreads", (int)maxThreads, 1, int.MaxValue);

#if !(USEBC || NETSTANDARD1X)
            byte[] B = Pbkdf2.ComputeDerivedKey(new HMACSHA256(P), S, 1, parallel * MFLen);
#else
            var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new NBitcoin.BouncyCastle.Crypto.Digests.Sha256Digest());
            mac.Init(new KeyParameter(P));
            byte[] B = Pbkdf2.ComputeDerivedKey(mac, S, 1, parallel * MFLen);
#endif
            uint[] B0 = new uint[B.Length / 4];
            for (int i = 0; i < B0.Length; i++)
            {
                B0[i] = BitPacking.UInt32FromLEBytes(B, i * 4);
            }             // code is easier with uint[]
            ThreadSMixCalls(B0, MFLen, cost, blockSize, parallel, (int)maxThreads);
            for (int i = 0; i < B0.Length; i++)
            {
                BitPacking.LEBytesFromUInt32(B0[i], B, i * 4);
            }
            Security.Clear(B0);

            return(B);
        }
Ejemplo n.º 21
0
        public void headlessUncompress(int[] @in, IntWrapper inpos, int inlength, int[] @out, IntWrapper outpos, int num)
        {
            int outlength = Util.greatestMultiple(num, BLOCK_SIZE);
            int tmpinpos  = inpos.get();
            int s         = outpos.get();

            for (; s + BLOCK_SIZE * 4 - 1 < outpos.get() + outlength; s += BLOCK_SIZE * 4)
            {
                int mbits1 = (int)((uint)@in[tmpinpos] >> 24);

                int mbits2 = (int)((uint)@in[tmpinpos] >> 16) & 0xFF;

                int mbits3 = (int)((uint)@in[tmpinpos] >> 8) & 0xFF;

                int mbits4 = (int)((uint)@in[tmpinpos]) & 0xFF;
                ++tmpinpos;
                BitPacking.fastunpack(@in, tmpinpos, @out, s, mbits1);
                tmpinpos += mbits1;
                BitPacking
                .fastunpack(@in, tmpinpos, @out, s + BLOCK_SIZE, mbits2);
                tmpinpos += mbits2;
                BitPacking.fastunpack(@in, tmpinpos, @out, s + 2 * BLOCK_SIZE,
                                      mbits3);
                tmpinpos += mbits3;
                BitPacking.fastunpack(@in, tmpinpos, @out, s + 3 * BLOCK_SIZE,
                                      mbits4);
                tmpinpos += mbits4;
            }
            for (; s < outpos.get() + outlength; s += BLOCK_SIZE)
            {
                int mbits = @in[tmpinpos];
                ++tmpinpos;
                BitPacking.fastunpack(@in, tmpinpos, @out, s, mbits);
                tmpinpos += mbits;
            }
            outpos.add(outlength);
            inpos.set(tmpinpos);
        }
Ejemplo n.º 22
0
        void ExpandKey(byte[] key)
        {
            Dispose();

            int[]   pc1 = PC1, rotations = Rotations, pc2 = PC2;
            uint[]  c   = new uint[16], d = new uint[16];
            ulong[] kex = new ulong[16];

            ulong k  = BitPacking.UInt64FromBEBytes(key, 0);
            ulong kp = Permute(PC1, k, 64); k = 0;

            uint cn = (uint)(kp >> 28);
            uint dn = (uint)(kp & 0xfffffff);

            kp = 0;

            for (int i = 0; i < c.Length; i++)
            {
                c[i] = cn = R(cn, rotations[i]);
            }
            cn = 0;

            for (int i = 0; i < c.Length; i++)
            {
                d[i] = dn = R(dn, rotations[i]);
            }
            dn = 0;

            for (int i = 0; i < kex.Length; i++)
            {
                ulong cd = (ulong)c[i] << 28 | d[i];
                kex[i] = Permute(PC2, cd, 56);
                cd     = 0;
            }
            Security.Clear(c); Security.Clear(d);

            Kex = kex;
        }
Ejemplo n.º 23
0
        public void verifyWithoutMask()
        {
            const int N     = 32;
            const int TIMES = 1000;
            Random    r     = new Random();

            int[] data         = new int[N];
            int[] compressed   = new int[N];
            int[] uncompressed = new int[N];
            for (int bit = 0; bit < 31; ++bit)
            {
                for (int t = 0; t < TIMES; ++t)
                {
                    for (int k = 0; k < N; ++k)
                    {
                        data[k] = r.Next(1 << bit);
                    }
                    BitPacking.fastpackwithoutmask(data, 0, compressed, 0, bit);
                    BitPacking.fastunpack(compressed, 0, uncompressed, 0, bit);
                    Assert2.assertArrayEquals(uncompressed, data);
                }
            }
        }
Ejemplo n.º 24
0
    void DoTest()
    {
        Debug.Log("write&read int : -11234\n");
        int reta = BitPacking.WriteInt32(a, arr, 0);

        Debug.Log(string.Format("write = {0}: {1}\t{2}\t{3}\t{4}\t{5}\n", reta, arr[0], arr[1], arr[2], arr[3], arr[4]));
        int retb = BitPacking.ReadInt32(ref b, brr, 0);

        Debug.Log(string.Format("read = {0}: {1}\n", retb, b));

        Debug.Log("write&read uint : 11234\n");
        int retc = BitPacking.WriteUInt32(c, crr, 0);

        Debug.Log(string.Format("write = {0}: {1}\t{2}\t{3}\t{4}\t{5}\n", retc, crr[0], crr[1], crr[2], crr[3], crr[4]));
        int retd = BitPacking.ReadUInt32(ref d, drr, 0);

        Debug.Log(string.Format("read = {0}: {1}\n", retd, d));

        if (reta != retb)
        {
            Debug.LogError("reta != retb");
        }
        if (a != b)
        {
            Debug.LogError("a != b");
        }
        if (retc != retd)
        {
            Debug.LogError("retc != retd");
        }
        if (c != d)
        {
            Debug.LogError("c != d");
        }
        Debug.Log("TestDone!\n");
    }
        public static void testWithDeltas(bool verbose)
        {
            const int N     = 32;
            const int times = 100000;

            Random r = new Random(0);

            int[] data         = new int[N];
            int[] compressed   = new int[N];
            int[] icompressed  = new int[N];
            int[] uncompressed = new int[N];

            for (int bit = 1; bit < 31; ++bit)
            {
                long comp    = 0;
                long decomp  = 0;
                long icomp   = 0;
                long idecomp = 0;

                for (int t = 0; t < times; ++t)
                {
                    data[0] = r.Next(1 << bit);
                    for (int k = 1; k < N; ++k)
                    {
                        data[k] = r.Next(1 << bit)
                                  + data[k - 1];
                    }

                    int[] tmpdata = Arrays.copyOf(data, data.Length);

                    long time1 = Port.System.nanoTime();
                    Delta.delta(tmpdata);
                    BitPacking.fastpackwithoutmask(tmpdata, 0,
                                                   compressed, 0, bit);
                    long time2 = Port.System.nanoTime();
                    BitPacking.fastunpack(compressed, 0,
                                          uncompressed, 0, bit);
                    Delta.fastinverseDelta(uncompressed);
                    long time3 = Port.System.nanoTime();
                    if (!Arrays.equals(data, uncompressed))
                    {
                        throw new Exception("bug");
                    }
                    comp   += time2 - time1;
                    decomp += time3 - time2;
                    tmpdata = Arrays.copyOf(data, data.Length);
                    time1   = Port.System.nanoTime();
                    IntegratedBitPacking.integratedpack(0, tmpdata,
                                                        0, icompressed, 0, bit);
                    time2 = Port.System.nanoTime();
                    IntegratedBitPacking.integratedunpack(0,
                                                          icompressed, 0, uncompressed, 0, bit);
                    time3 = Port.System.nanoTime();
                    if (!Arrays.equals(icompressed, compressed))
                    {
                        throw new Exception("ibug " + bit);
                    }

                    if (!Arrays.equals(data, uncompressed))
                    {
                        throw new Exception("bug " + bit);
                    }
                    icomp   += time2 - time1;
                    idecomp += time3 - time2;
                }

                if (verbose)
                {
                    Console.WriteLine("bit = "
                                      + bit
                                      + " comp. speed = "
                                      + (N * times * 1000.0 / (comp)).ToString("0")
                                      + " decomp. speed = "
                                      + (N * times * 1000.0 / (decomp)).ToString("0")
                                      + " icomp. speed = "
                                      + (N * times * 1000.0 / (icomp)).ToString("0")
                                      + " idecomp. speed = "
                                      + (N * times * 1000.0 / (idecomp)).ToString("0"));
                }
            }
        }
Ejemplo n.º 26
0
        private void decodePage(int[] @in, IntWrapper inpos, int[] @out, IntWrapper outpos, int thissize)
        {
            int initpos = inpos.get();

            int wheremeta = @in[inpos.get()];

            inpos.increment();
            int inexcept = initpos + wheremeta;

            int bytesize = @in[inexcept++];

            byteContainer.clear();
            //byteContainer.asIntBuffer().put(@in, inexcept, (bytesize + 3) / 4); //Port note: I've collapsed the code here
            byteContainer.put(@in, inexcept, (bytesize + 3) / 4);
            byteContainer._ms.Position = 0;
            inexcept += (bytesize + 3) / 4;

            int bitmap = @in[inexcept++];

            for (int k = 2; k <= 32; ++k)
            {
                if ((bitmap & (1 << (k - 1))) != 0)
                {
                    int size      = @in[inexcept++];
                    int roundedup = Util
                                    .greatestMultiple(size + 31, 32);
                    if (dataTobePacked[k].Length < roundedup)
                    {
                        dataTobePacked[k] = new int[roundedup];
                    }
                    if (inexcept + roundedup / 32 * k <= @in.Length)
                    {
                        int j = 0;
                        for (; j < size; j += 32)
                        {
                            BitPacking.fastunpack(@in, inexcept,
                                                  dataTobePacked[k], j, k);
                            inexcept += k;
                        }
                        int overflow = j - size;
                        inexcept -= overflow * k / 32;
                    }
                    else
                    {
                        int   j            = 0;
                        int[] buf          = new int[roundedup / 32 * k];
                        int   initinexcept = inexcept;
                        Array.Copy(@in, inexcept, buf, 0, @in.Length - inexcept);
                        for (; j < size; j += 32)
                        {
                            BitPacking.fastunpack(buf, inexcept - initinexcept,
                                                  dataTobePacked[k], j, k);
                            inexcept += k;
                        }
                        int overflow = j - size;
                        inexcept -= overflow * k / 32;
                    }
                }
            }
            Arrays.fill(dataPointers, 0);
            int tmpoutpos = outpos.get();
            int tmpinpos  = inpos.get();

            for (int run = 0, run_end = thissize / BLOCK_SIZE; run < run_end; ++run, tmpoutpos += BLOCK_SIZE)
            {
                int b = byteContainer.get();

                int cexcept = byteContainer.get() & 0xFF;
                for (int k = 0; k < BLOCK_SIZE; k += 32)
                {
                    BitPacking.fastunpack(@in, tmpinpos, @out,
                                          tmpoutpos + k, b);
                    tmpinpos += b;
                }
                if (cexcept > 0)
                {
                    int maxbits = byteContainer.get();

                    int index = maxbits - b;
                    if (index == 1)
                    {
                        for (int k = 0; k < cexcept; ++k)
                        {
                            int pos = byteContainer.get() & 0xFF;
                            @out[pos + tmpoutpos] |= 1 << b;
                        }
                    }
                    else
                    {
                        for (int k = 0; k < cexcept; ++k)
                        {
                            int pos = byteContainer.get() & 0xFF;

                            int exceptvalue = dataTobePacked[index][dataPointers[index]++];
                            @out[pos + tmpoutpos] |= exceptvalue << b;
                        }
                    }
                }
            }
            outpos.set(tmpoutpos);
            inpos.set(inexcept);
        }
Ejemplo n.º 27
0
        private void encodePage(int[] @in, IntWrapper inpos, int thissize, int[] @out, IntWrapper outpos)
        {
            int headerpos = outpos.get();

            outpos.increment();
            int tmpoutpos = outpos.get();

            // Clear working area.
            Arrays.fill(dataPointers, 0);
            byteContainer.clear();

            int tmpinpos = inpos.get();

            for (int finalinpos = tmpinpos + thissize - BLOCK_SIZE; tmpinpos <= finalinpos; tmpinpos += BLOCK_SIZE)
            {
                getBestBFromData(@in, tmpinpos);

                int tmpbestb = bestbbestcexceptmaxb[0];
                byteContainer.put((sbyte)bestbbestcexceptmaxb[0]);
                byteContainer.put((sbyte)bestbbestcexceptmaxb[1]);
                if (bestbbestcexceptmaxb[1] > 0)
                {
                    byteContainer.put((sbyte)bestbbestcexceptmaxb[2]);

                    int index = bestbbestcexceptmaxb[2]
                                - bestbbestcexceptmaxb[0];
                    if (dataPointers[index]
                        + bestbbestcexceptmaxb[1] >= dataTobePacked[index].Length)
                    {
                        int newsize = 2 * (dataPointers[index] + bestbbestcexceptmaxb[1]);
                        // make sure it is a multiple of 32
                        newsize = Util.greatestMultiple(newsize + 31, 32);
                        dataTobePacked[index] = Arrays.copyOf(dataTobePacked[index], newsize);
                    }
                    for (int k = 0; k < BLOCK_SIZE; ++k)
                    {
                        if ((int)((uint)@in[k + tmpinpos] >> bestbbestcexceptmaxb[0]) != 0)
                        {
                            // we have an exception
                            byteContainer.put((sbyte)k);
                            dataTobePacked[index][dataPointers[index]++] = (int)((uint)@in[k + tmpinpos] >> tmpbestb);
                        }
                    }
                }
                for (int k = 0; k < BLOCK_SIZE; k += 32)
                {
                    BitPacking.fastpack(@in, tmpinpos + k, @out,
                                        tmpoutpos, tmpbestb);
                    tmpoutpos += tmpbestb;
                }
            }
            inpos.set(tmpinpos);
            @out[headerpos] = tmpoutpos - headerpos;

            int bytesize = byteContainer.position();

            while ((byteContainer.position() & 3) != 0)
            {
                byteContainer.put((sbyte)0);
            }
            @out[tmpoutpos++] = bytesize;

            int howmanyints = byteContainer.position() / 4;

            byteContainer.flip();
            byteContainer.asIntBuffer().get(@out, tmpoutpos, howmanyints);
            tmpoutpos += howmanyints;
            int bitmap = 0;

            for (int k = 2; k <= 32; ++k)
            {
                if (dataPointers[k] != 0)
                {
                    bitmap |= (1 << (k - 1));
                }
            }
            @out[tmpoutpos++] = bitmap;

            for (int k = 2; k <= 32; ++k)
            {
                if (dataPointers[k] != 0)
                {
                    @out[tmpoutpos++] = dataPointers[k];// size
                    int j = 0;
                    for (; j < dataPointers[k]; j += 32)
                    {
                        BitPacking.fastpack(dataTobePacked[k],
                                            j, @out, tmpoutpos, k);
                        tmpoutpos += k;
                    }
                    int overflow = j - dataPointers[k];
                    tmpoutpos -= overflow * k / 32;
                }
            }
            outpos.set(tmpoutpos);
        }
Ejemplo n.º 28
0
 private static int unpack(int[] inBuf, int inOff, int[] outBuf, int outOff, int validBits)
 {
     BitPacking.fastunpack(inBuf, inOff, outBuf, outOff, validBits);
     return(validBits);
 }