Exemplo n.º 1
0
        /// <summary>
        /// Decrypts the Identity Master Key (IMK) for for the given <paramref name="identityUniqueId"/>
        /// using the provided <paramref name="quickPass"/> and returns it. If <c>null</c> is passed for
        /// <paramref name="identityUniqueId"/>, the decrypted IML for the current identity will be returned.
        /// </summary>
        /// <param name="quickPass">The QuickPass (first x characters from the identity's master password
        /// which was used to encrypt the IMK when setting the QuickPass entry for the identity.</param>
        /// <param name="identityUniqueId">The hex representation of the identity's unique id (block 0),
        /// or <c>null</c> if you want to decrypt the current identity's IMK.</param>
        /// <param name="progress">An object implementing the IProgress interface for tracking the operation's progress (optional).</param>
        /// <param name="progressText">A string representing a text descrition for the progress indicator (optional).</param>
        /// <returns>The decrypted block 1 keys (IMK, ILK) for the given <paramref name="identityUniqueId"/>, or
        /// <c>null</c> if no such QuickPass entry exists.</returns>
        public async Task <QuickPassDecryptedKeys> GetQuickPassDecryptedImk(string quickPass, string identityUniqueId        = null,
                                                                            IProgress <KeyValuePair <int, string> > progress = null, string progressText = null)
        {
            QuickPassItem qpi = null;

            if (identityUniqueId == null)
            {
                if (_identityManager.CurrentIdentity == null)
                {
                    return(null);
                }
                identityUniqueId = _identityManager.CurrentIdentity?.Block0?.UniqueIdentifier?.ToHex();
            }


            if (identityUniqueId == null)
            {
                Log.Error("Could not resolve current identity in {MethodName}, throwing Exception!",
                          nameof(GetQuickPassDecryptedImk));

                throw new InvalidOperationException("Cannot return QuickPass-decrypted IMK without an identity!");
            }

            lock (_dataSyncObj)
            {
                if (!_quickPassItems.ContainsKey(identityUniqueId))
                {
                    Log.Warning("No identity found for id {IdentityUniqueId} in {MethodName}",
                                identityUniqueId, nameof(GetQuickPassDecryptedImk));

                    return(null);
                }

                qpi = _quickPassItems[identityUniqueId];
            }

            byte[] key = await SQRL.EnScryptCT(quickPass, qpi.ScryptRandomSalt, (int)Math.Pow(2, 9),
                                               qpi.ScryptIterationCount, progress, progressText);

            byte[] decryptedImk = StreamEncryption.Decrypt(qpi.EncryptedImk, qpi.Nonce, key);
            byte[] decryptedIlk = StreamEncryption.Decrypt(qpi.EncryptedIlk, qpi.Nonce, key);

            Log.Information("QuickPass retrieved for identity {IdentityUniqueId}",
                            identityUniqueId);

            return(new QuickPassDecryptedKeys(decryptedImk, decryptedIlk));
        }
Exemplo n.º 2
0
        public async void EnScryptTest()
        {
            using (WebClient wc = new WebClient())
            {
                String   enScryptVectors = wc.DownloadString("https://raw.githubusercontent.com/sqrldev/sqrl-test-vectors/master/vectors/enscrypt-vectors.txt");
                String[] lines           = enScryptVectors.Split(Environment.NewLine);
                bool     first           = true;

                foreach (var line in lines)
                {
                    if (first || string.IsNullOrEmpty(line))
                    {
                        first = false;
                        continue;
                    }
                    string[] data = line.Replace("\"", "").Split(',');
                    byte[]   ary  = await SQRL.EnScryptCT(data[0], Encoding.UTF8.GetBytes(data[1]), (int)Math.Pow(2, 9), int.Parse(data[2]));

                    string hex    = data[4];
                    string result = Sodium.Utilities.BinaryToHex(ary);
                    Assert.Equal(hex.CleanUpString(), result.CleanUpString());
                }
            }
        }