示例#1
0
        private unsafe void SetBip32(byte[] mnemonic)
        {
            HmacSha512 hmac = new HmacSha512(mnemonic);

            byte[] salt        = Encoding.UTF8.GetBytes($"mnemonic{passPhrase?.Normalize(NormalizationForm.FormKD)}");
            byte[] saltForHmac = new byte[salt.Length + 4];
            Buffer.BlockCopy(salt, 0, saltForHmac, 0, salt.Length);

            byte[] seed = new byte[64];

            fixed(byte *saltPt = &saltForHmac[salt.Length])
            {
                // F()
                byte[] resultOfF = new byte[hmac.OutputSize];

                // Concatinate i after salt
                //saltPt[0] = (byte)(1 >> 24);
                //saltPt[1] = (byte)(1 >> 16);
                //saltPt[2] = (byte)(1 >> 8);
                saltPt[3] = 1;

                // compute u1
                byte[] u1 = hmac.ComputeHash(saltForHmac);

                Buffer.BlockCopy(u1, 0, resultOfF, 0, u1.Length);

                // compute u2 to u(c-1) where c is iteration and each u is the hmac of previous u
                for (int j = 1; j < 2048; j++)
                {
                    u1 = hmac.ComputeHash(u1);

                    // result of F() is XOR sum of all u arrays
                    int len = u1.Length;
                    fixed(byte *first = resultOfF, second = u1)
                    {
                        byte *fp = first;
                        byte *sp = second;

                        *(ulong *)fp        ^= *(ulong *)sp;
                        *(ulong *)(fp + 8)  ^= *(ulong *)(sp + 8);
                        *(ulong *)(fp + 16) ^= *(ulong *)(sp + 16);
                        *(ulong *)(fp + 24) ^= *(ulong *)(sp + 24);
                        *(ulong *)(fp + 32) ^= *(ulong *)(sp + 32);
                        *(ulong *)(fp + 40) ^= *(ulong *)(sp + 40);
                        *(ulong *)(fp + 48) ^= *(ulong *)(sp + 48);
                        *(ulong *)(fp + 56) ^= *(ulong *)(sp + 56);
                    }
                }

                Buffer.BlockCopy(resultOfF, 0, seed, 0, resultOfF.Length);
            }
        }
示例#2
0
        public void ComputeHash_WithKey_ExceptionTest()
        {
            HmacSha512 hmac = new HmacSha512();

            Exception ex = Assert.Throws <ArgumentNullException>(() => hmac.ComputeHash(null, new byte[1]));

            Assert.Contains("Data can not be null", ex.Message);

            ex = Assert.Throws <ArgumentNullException>(() => hmac.ComputeHash(new byte[1], null));
            Assert.Contains("Key can not be null", ex.Message);

            hmac.Dispose();
            ex = Assert.Throws <ObjectDisposedException>(() => hmac.ComputeHash(new byte[1], new byte[1]));
        }
示例#3
0
        public void ComputeHash_Reuse_Test(byte[] key1, byte[] data1_1, byte[] exp1_1, byte[] data1_2, byte[] exp1_2,
                                           byte[] key2, byte[] data2_1, byte[] exp2_1, byte[] data2_2, byte[] exp2_2)
        {
            using HmacSha512 hmac = new HmacSha512();
            byte[] actual1_1 = hmac.ComputeHash(data1_1, key1);
            Assert.Equal(exp1_1, actual1_1);

            byte[] actual2_1 = hmac.ComputeHash(data2_1, key2); // use different key on each call
            Assert.Equal(exp2_1, actual2_1);

            byte[] actual1_2 = hmac.ComputeHash(data1_2, key1);
            Assert.Equal(exp1_2, actual1_2);

            byte[] actual2_2 = hmac.ComputeHash(data2_2, key2);
            Assert.Equal(exp2_2, actual2_2);
        }
示例#4
0
        private MnemonicType GetMnomonicType(string normalizedMn)
        {
            using HmacSha512 hmac = new HmacSha512(Encoding.UTF8.GetBytes("Seed version"));
            byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(normalizedMn));

            if (hash[0] == 1)
            {
                return(MnemonicType.Standard);
            }
            else if (hash[0] == 0x10)
            {
                int second = hash[1] & 0xf0;
                if (second == 0)
                {
                    return(MnemonicType.SegWit);
                }
                else if (second == 0x10)
                {
                    return(MnemonicType.Legacy2Fa);
                }
                else if (second == 0x20)
                {
                    return(MnemonicType.SegWit2Fa);
                }
            }

            return(MnemonicType.Undefined);
        }
示例#5
0
        public void ComputeHash_ExceptionTest()
        {
            HmacSha512 hmac = new HmacSha512();

            Exception ex = Assert.Throws <ArgumentNullException>(() => hmac.ComputeHash(null));

            Assert.Contains("Data can not be null", ex.Message);

            ex = Assert.Throws <ArgumentNullException>(() => hmac.ComputeHash(new byte[1]));
            Assert.Contains("Key must be set before calling this function", ex.Message);

            hmac.Key = new byte[1];
            ex       = Assert.Throws <ArgumentNullException>(() => hmac.ComputeHash(null));
            Assert.Contains("Data can not be null", ex.Message);

            hmac.Dispose();
            ex = Assert.Throws <ObjectDisposedException>(() => hmac.ComputeHash(new byte[1]));
        }
示例#6
0
        public void ComputeHash_CtorKey_ReuseTest(byte[] key1, byte[] data1_1, byte[] exp1_1, byte[] data1_2, byte[] exp1_2,
                                                  byte[] key2, byte[] data2_1, byte[] exp2_1, byte[] data2_2, byte[] exp2_2)
        {
            using HmacSha512 hmac = new HmacSha512(key1);
            byte[] actual1_1 = hmac.ComputeHash(data1_1);
            Assert.Equal(exp1_1, actual1_1);

            byte[] actual1_2 = hmac.ComputeHash(data1_2);
            Assert.Equal(exp1_2, actual1_2);

            // change key
            hmac.Key = key2;
            byte[] actual2_1 = hmac.ComputeHash(data2_1);
            Assert.Equal(exp2_1, actual2_1);

            byte[] actual2_2 = hmac.ComputeHash(data2_2);
            Assert.Equal(exp2_2, actual2_2);
        }
示例#7
0
        public void ComputeHash_Empty_WithKey_Test(byte[] key, byte[] data)
        {
            using var sysHmac = new System.Security.Cryptography.HMACSHA512(key);
            byte[] expected = sysHmac.ComputeHash(data);

            using HmacSha512 hmac = new HmacSha512();
            byte[] actual = hmac.ComputeHash(data, key);
            Assert.Equal(expected, actual);
        }
示例#8
0
 public void ComputeHash_NIST_CtorKey_Test(byte[] msg, byte[] key, byte[] expected, int len, bool truncate)
 {
     using HmacSha512 hmac = new HmacSha512(key);
     byte[] actual = hmac.ComputeHash(msg);
     if (truncate)
     {
         byte[] temp = new byte[len];
         Buffer.BlockCopy(actual, 0, temp, 0, len);
         actual = temp;
     }
     Assert.Equal(expected, actual);
 }
示例#9
0
 public void ComputeHash_rfc_CtorKey_Test(byte[] msg, byte[] key, byte[] expected)
 {
     using HmacSha512 hmac = new HmacSha512(key);
     byte[] actual = hmac.ComputeHash(msg);
     Assert.Equal(expected, actual);
 }
 public byte[] Library_HmacSha512(byte[] data) => libHmac.ComputeHash(data);
示例#11
0
 private void HmacAndSplitData(byte[] data, byte[] key, out byte[] left32, out byte[] right32)
 {
     byte[] hash = hmac.ComputeHash(data, key);
     left32  = hash.SubArray(0, 32);
     right32 = hash.SubArray(32);
 }