示例#1
0
        public void Reset(byte[] key)
        {
            if (key.Length != 32)
            {
                throw new ArgumentException("Poly1305 key must be 256 bits.");
            }

            bufferLength = 0;

            h0 = h1 = h2 = h3 = h4 = 0;

            // Extract r portion of key (and "clamp" the values)
            uint t0 = Lend.Pack32(key, 0);
            uint t1 = Lend.Pack32(key, 4);
            uint t2 = Lend.Pack32(key, 8);
            uint t3 = Lend.Pack32(key, 12);

            // NOTE: The masks perform the key "clamping" implicitly
            r0 = t0 & 0x03FFFFFFU;
            r1 = ((t0 >> 26) | (t1 << 6)) & 0x03FFFF03U;
            r2 = ((t1 >> 20) | (t2 << 12)) & 0x03FFC0FFU;
            r3 = ((t2 >> 14) | (t3 << 18)) & 0x03F03FFFU;
            r4 = (t3 >> 8) & 0x000FFFFFU;

            // Precompute multipliers
            s1 = r1 * 5;
            s2 = r2 * 5;
            s3 = r3 * 5;
            s4 = r4 * 5;

            k0 = Lend.Pack32(key, BLOCK_SIZE + 0);
            k1 = Lend.Pack32(key, BLOCK_SIZE + 4);
            k2 = Lend.Pack32(key, BLOCK_SIZE + 8);
            k3 = Lend.Pack32(key, BLOCK_SIZE + 12);
        }
示例#2
0
        private static void StateInit(uint[] state, byte[] key, byte[] nonce)
        {
            Array.Clear(state, 0, SIZE);

            state[0] = SIGMA[0];
            state[1] = SIGMA[1];
            state[2] = SIGMA[2];
            state[3] = SIGMA[3];

            Lend.Pack32(key, 0, state, 4, 8);
            Lend.Pack32(nonce, 0, state, 13, 3);
        }
示例#3
0
        private void ProcessBuffer()
        {
            if (bufferLength == 0)
            {
                return;
            }
            if (bufferLength < BLOCK_SIZE)
            {
                buffer[bufferLength] = 1;
                Array.Clear(buffer, bufferLength + 1, BLOCK_SIZE - bufferLength - 1);
            }

            ulong t0 = Lend.Pack32(buffer, 0);
            ulong t1 = Lend.Pack32(buffer, 4);
            ulong t2 = Lend.Pack32(buffer, 8);
            ulong t3 = Lend.Pack32(buffer, 12);

            h0 += (uint)(t0 & 0x3ffffffU);
            h1 += (uint)((((t1 << 32) | t0) >> 26) & 0x3ffffff);
            h2 += (uint)((((t2 << 32) | t1) >> 20) & 0x3ffffff);
            h3 += (uint)((((t3 << 32) | t2) >> 14) & 0x3ffffff);
            h4 += (uint)(t3 >> 8);

            if (bufferLength == BLOCK_SIZE)
            {
                h4 += (1 << 24);
            }

            ulong tp0 = mul32x32_64(h0, r0) + mul32x32_64(h1, s4) + mul32x32_64(h2, s3) + mul32x32_64(h3, s2) + mul32x32_64(h4, s1);
            ulong tp1 = mul32x32_64(h0, r1) + mul32x32_64(h1, r0) + mul32x32_64(h2, s4) + mul32x32_64(h3, s3) + mul32x32_64(h4, s2);
            ulong tp2 = mul32x32_64(h0, r2) + mul32x32_64(h1, r1) + mul32x32_64(h2, r0) + mul32x32_64(h3, s4) + mul32x32_64(h4, s3);
            ulong tp3 = mul32x32_64(h0, r3) + mul32x32_64(h1, r2) + mul32x32_64(h2, r1) + mul32x32_64(h3, r0) + mul32x32_64(h4, s4);
            ulong tp4 = mul32x32_64(h0, r4) + mul32x32_64(h1, r3) + mul32x32_64(h2, r2) + mul32x32_64(h3, r1) + mul32x32_64(h4, r0);

            h0  = (uint)tp0 & 0x3ffffff; tp1 += (tp0 >> 26);
            h1  = (uint)tp1 & 0x3ffffff; tp2 += (tp1 >> 26);
            h2  = (uint)tp2 & 0x3ffffff; tp3 += (tp2 >> 26);
            h3  = (uint)tp3 & 0x3ffffff; tp4 += (tp3 >> 26);
            h4  = (uint)tp4 & 0x3ffffff;
            h0 += (uint)(tp4 >> 26) * 5;
            h1 += (h0 >> 26); h0 &= 0x3ffffff;

            bufferLength = 0;
        }