public void OpenDetachedSecretBoxBadNonce()
        {
            var actual = SecretBox.CreateDetached(
                Encoding.UTF8.GetBytes("Adam Caudill"),
                Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWX"),
                Encoding.UTF8.GetBytes("12345678901234567890123456789012"));

            SecretBox.OpenDetached(actual.CipherText, actual.Mac,
                                   Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVW"),
                                   Encoding.UTF8.GetBytes("12345678901234567890123456789012"));
        }
Пример #2
0
        public void OpenDetachedSecretBoxBadKey()
        {
            var actual = SecretBox.CreateDetached(
                Encoding.UTF8.GetBytes("Adam Caudill"),
                Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWX"),
                Encoding.UTF8.GetBytes("12345678901234567890123456789012"));

            Assert.Throws <KeyOutOfRangeException>(() =>
            {
                SecretBox.OpenDetached(actual.CipherText, actual.Mac,
                                       Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWX"),
                                       Encoding.UTF8.GetBytes("123456789012345678901234567890"));
            });
        }
Пример #3
0
        public void DetachedSecretBox()
        {
            var expected = Utilities.HexToBinary("4164616d2043617564696c6c");
            var actual   = SecretBox.CreateDetached(
                Encoding.UTF8.GetBytes("Adam Caudill"),
                Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWX"),
                Encoding.UTF8.GetBytes("12345678901234567890123456789012"));

            var clear = SecretBox.OpenDetached(actual.CipherText, actual.Mac,
                                               Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWX"),
                                               Encoding.UTF8.GetBytes("12345678901234567890123456789012"));

            Assert.AreEqual(clear, expected);
        }
Пример #4
0
        public static KeyPair ImportFromKeystore(Wallet wallet, string input, int len, JsonSerializerSettings jsonSettings)
        {
            Contract.Assert(!string.IsNullOrWhiteSpace(wallet.PasswordHash));
            var passwordHashBytes = Encoding.UTF8.GetBytes(wallet.PasswordHash);

            var kstore = JsonConvert.DeserializeObject <Keystore>(input, jsonSettings);

            if (kstore.Version != KeystoreVersion)
            {
                throw new NotSupportedException("Unsupported version");
            }

            string kdfTypeString = kstore.Crypto.Kdf;

            if (!Enum.TryParse(kdfTypeString, true, out KdfType kdfType))
            {
                throw new NotSupportedException("Unsupported kdf");
            }

            byte[] derivedKey;
            switch (kdfType)
            {
            case KdfType.Scrypt:
            {
                derivedKey = PasswordHash.ScryptHashBinary(
                    passwordHashBytes,
                    kstore.Crypto.KdfParameters.Salt.FromHex(),
                    PasswordHash.Strength.Sensitive,
                    kstore.Crypto.KdfParameters.DerivedKeyLength);
                break;
            }

            case KdfType.Argon:
            {
                derivedKey = PasswordHash.ArgonHashBinary(
                    passwordHashBytes,
                    kstore.Crypto.KdfParameters.Salt.FromHex(),
                    PasswordHash.StrengthArgon.Sensitive,
                    kstore.Crypto.KdfParameters.DerivedKeyLength);
                break;
            }

            default:
                throw new NotSupportedException("Unsupported kdf");
            }

            string ciphertext = kstore.Crypto.CipherText;
            string mac        = kstore.Crypto.Mac;
            string iv         = kstore.Crypto.CipherParameters.Iv;

            byte[] nonce = derivedKey.Take(len).ToArray();
            byte[] seed  = SecretBox.OpenDetached(ciphertext, mac.FromHex(), nonce, iv.FromHex());

            var imported = new KeyPair(
                wallet.KeyPairs.Count + 1,
                kstore.Address.FromHex(),
                seed);

            wallet.KeyPairs.Add(imported);
            return(imported);
        }