Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public void Constructor_ExceptionTest()
        {
            Assert.Throws <ArgumentNullException>(() => new HmacSha512(null));

            HmacSha512 hmac = new HmacSha512();

            Assert.Throws <ArgumentNullException>(() => hmac.Key = null);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of <see cref="BIP0085"/>.
        /// </summary>
        /// <exception cref="ArgumentException"/>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="ArgumentOutOfRangeException"/>
        /// <exception cref="FormatException"/>
        /// <param name="masterExtendedKey">Master extended key (xprv string)</param>
        /// <param name="netType">
        /// [Default value = <see cref="NetworkType.MainNet"/>]
        /// The expected network that this extended key belongs to.
        /// </param>
        public BIP0085(string masterExtendedKey, NetworkType netType = NetworkType.MainNet)
        {
            bip32 = new BIP0032(masterExtendedKey, netType);
            ReadOnlySpan <byte> empty = new byte[4];

            if (!empty.SequenceEqual(bip32.ChildNumber) || !empty.SequenceEqual(bip32.ParentFingerPrint))
            {
                throw new ArgumentException("BIP-85 is only defined for master extended keys.", nameof(masterExtendedKey));
            }
            hmac = new HmacSha512();
        }
Exemplo n.º 5
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);
 }
Exemplo n.º 6
0
        public static void Properties()
        {
            var a = new HmacSha512();

            Assert.Equal(64, a.MinKeySize);
            Assert.Equal(64, a.DefaultKeySize);
            Assert.Equal(int.MaxValue, a.MaxKeySize);

            Assert.Equal(16, a.MinMacSize);
            Assert.Equal(64, a.DefaultMacSize);
            Assert.Equal(64, a.MaxMacSize);
        }
Exemplo n.º 7
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);
            }
        }
Exemplo n.º 8
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]));
        }
Exemplo n.º 9
0
        public static void Test(string key, string msg, string mac)
        {
            var a = new HmacSha512();

            using (var k = Key.Import(a, key.DecodeHex(), KeyBlobFormat.RawSymmetricKey))
            {
                var m = msg.DecodeHex();

                var expected = mac.DecodeHex();
                var actual   = a.Sign(k, m, expected.Length);

                Assert.Equal(expected, actual);
                Assert.True(a.TryVerify(k, m, expected));
            }
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        public static void Properties()
        {
            var a = new HmacSha512();

            Assert.Equal(64, HmacSha512.MinKeySize);
            Assert.Equal(64, HmacSha512.MaxKeySize);
            Assert.Equal(16, HmacSha512.MinMacSize);
            Assert.Equal(64, HmacSha512.MaxMacSize);

            Assert.Equal(64, a.KeySize);
            Assert.Equal(64, a.MacSize);

            Assert.Equal(64, MacAlgorithm.HmacSha512_256.KeySize);
            Assert.Equal(32, MacAlgorithm.HmacSha512_256.MacSize);

            Assert.Equal(64, MacAlgorithm.HmacSha512.KeySize);
            Assert.Equal(64, MacAlgorithm.HmacSha512.MacSize);
        }
Exemplo n.º 12
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]));
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Releases the resources used by the <see cref="BIP0032"/> class.
        /// </summary>
        /// <param name="disposing">
        /// True to release both managed and unmanaged resources; false to release only unmanaged resources.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    if (!(hmac is null))
                    {
                        hmac.Dispose();
                    }
                    hmac = null;

                    if (!(PrvKey is null))
                    {
                        PrvKey.Dispose();
                    }
                    PrvKey = null;

                    PubKey = null;

                    if (ChainCode != null)
                    {
                        Array.Clear(ChainCode, 0, ChainCode.Length);
                    }
                    ChainCode = null;

                    if (ParentFingerPrint != null)
                    {
                        Array.Clear(ParentFingerPrint, 0, ParentFingerPrint.Length);
                    }
                    ParentFingerPrint = null;

                    if (ChildNumber != null)
                    {
                        Array.Clear(ChildNumber, 0, ChildNumber.Length);
                    }
                    ChildNumber = null;

                    b58Encoder = null;
                }

                isDisposed = true;
            }
        }
Exemplo n.º 15
0
        public void ConstructorTest(byte[] key, bool isKeyHashed)
        {
            using var sysHmac = new System.Security.Cryptography.HMACSHA512(key);
            byte[] expectedKey = sysHmac.Key;

            // Set key in constructor:
            using (HmacSha512 hmac = new HmacSha512(key))
            {
                Assert.Equal(expectedKey, hmac.Key);
                if (!isKeyHashed)
                {
                    Assert.Equal(key, hmac.Key);
                    // Make sure small unhashed keys are cloned
                    Assert.NotSame(key, hmac.Key);
                }
                else
                {
                    Assert.NotEqual(key, hmac.Key);
                }
            }

            // Set key using the property:
            using (HmacSha512 hmac = new HmacSha512())
            {
                Assert.Null(hmac.Key);
                hmac.Key = key;
                if (!isKeyHashed)
                {
                    Assert.Equal(key, hmac.Key);
                    // Make sure small unhashed keys are cloned
                    Assert.NotSame(key, hmac.Key);
                }
                else
                {
                    Assert.NotEqual(key, hmac.Key);
                }
            }
        }
Exemplo n.º 16
0
        public static byte[] GenerateChecksum(string checksum, int offset, byte[] buffer, int eof = -1)
        {
            byte[] returnValue = null;
            switch (checksum)
            {
            case "Adler8 - {1Bytes}":
                returnValue = eof == -1 ? Adler8.Compute(offset, buffer) : Adler8.Compute(offset, buffer, eof);
                break;

            case "Adler16 - {2Bytes}":
                returnValue = eof == -1 ? Adler16.Compute(offset, buffer) : Adler16.Compute(offset, buffer, eof);
                break;

            case "Adler32 - {4Bytes}":
                returnValue = eof == -1 ? Adler32.Compute(offset, buffer) : Adler32.Compute(offset, buffer, eof);
                break;

            case "Checksum8 - {1Bytes}":
                returnValue = eof == -1 ? Checksum8.Compute(offset, buffer) : Checksum8.Compute(offset, buffer, eof);
                break;

            case "Checksum16 - {2Bytes}":
                returnValue = eof == -1 ? Checksum16.Compute(offset, buffer) : Checksum16.Compute(offset, buffer, eof);
                break;

            case "Checksum24 - {3Bytes}":
                returnValue = eof == -1 ? Checksum24.Compute(offset, buffer) : Checksum24.Compute(offset, buffer, eof);
                break;

            case "Checksum32 - {4Bytes}":
                returnValue = eof == -1 ? Checksum32.Compute(offset, buffer) : Checksum32.Compute(offset, buffer, eof);
                break;

            case "Checksum40 - {5Bytes}":
                returnValue = eof == -1 ? Checksum40.Compute(offset, buffer) : Checksum40.Compute(offset, buffer, eof);
                break;

            case "Checksum48 - {6Bytes}":
                returnValue = eof == -1 ? Checksum48.Compute(offset, buffer) : Checksum48.Compute(offset, buffer, eof);
                break;

            case "Checksum56 - {7Bytes}":
                returnValue = eof == -1 ? Checksum56.Compute(offset, buffer) : Checksum56.Compute(offset, buffer, eof);
                break;

            case "Checksum64 - {8Bytes}":
                returnValue = eof == -1 ? Checksum64.Compute(offset, buffer) : Checksum64.Compute(offset, buffer, eof);
                break;

            case "CRC16 - {2Bytes}":
                Crc16 crc16 = new Crc16();
                returnValue = eof == -1 ? crc16.Compute(offset, buffer) : crc16.Compute(offset, buffer, eof);
                break;

            case "CRC16 CCITT - {2Bytes}":
                Crc16ccitt crc16Ccitt = new Crc16ccitt();
                returnValue = eof == -1 ? crc16Ccitt.Compute(offset, buffer) : crc16Ccitt.Compute(offset, buffer, eof);
                break;

            case "CRC32 - {4Bytes}":
                returnValue = eof == -1 ? Crc32.Compute(offset, buffer) : Crc32.Compute(offset, buffer, eof);
                break;

            case "HMAC SHA 1 (128)  - {16Bytes}":
                returnValue = eof == -1 ? HmacSha1.Compute(offset, buffer) : HmacSha1.Compute(offset, buffer, eof);
                break;

            case "HMAC SHA 256 - {32Bytes}":
                returnValue = eof == -1 ? HmacSha256.Compute(offset, buffer) : HmacSha256.Compute(offset, buffer, eof);
                break;

            case "HMAC SHA 384 - {48Bytes}":
                returnValue = eof == -1 ? HmacSha384.Compute(offset, buffer) : HmacSha384.Compute(offset, buffer, eof);
                break;

            case "HMAC SHA 512 - {64Bytes}":
                returnValue = eof == -1 ? HmacSha512.Compute(offset, buffer) : HmacSha512.Compute(offset, buffer, eof);
                break;

            case "MD5 - {16Bytes}":
                returnValue = eof == -1 ? Md5.Compute(offset, buffer) : Md5.Compute(offset, buffer, eof);
                break;

            case "MD5 CNG - {16Bytes}":
                returnValue = eof == -1 ? Md5Cng.Compute(offset, buffer) : Md5Cng.Compute(offset, buffer, eof);
                break;
            }
            return(returnValue);
        }
Exemplo n.º 17
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);
 }