Beispiel #1
0
 public MAC16 AESDecryptMAC(CipherV2 cipherV2, RandomKey32 randomKey, IVCache ivCache, LongRunningOperationContext context)
 {
     Guard.NotNull(new object[] { cipherV2, randomKey, context });
     byte[] macBytes;
     if (cipherV2.RoundsExponent.Value == RoundsExponent.DontMakeRounds)
     {
         macBytes = this._platform.ComputeAESRound(AESDir.Decrpyt, cipherV2.IV16.GetBytes(), cipherV2.MACCipher16.GetBytes(), randomKey.GetBytes());
     }
     else
     {
         context.EncryptionProgress.Message = LocalizableStrings.MsgDecryptingMAC;
         macBytes = ComputeAESWithRounds(AESDir.Decrpyt, cipherV2.IV16, cipherV2.MACCipher16.GetBytes(), randomKey.GetBytes(), cipherV2.RoundsExponent.Value, ivCache, context);
     }
     return(new MAC16(macBytes));
 }
Beispiel #2
0
        /// <summary>
        /// Purpose:
        /// </summary>
        /// <param name="iv"></param>
        /// <param name="roundsExp"></param>
        /// <returns></returns>
        public IVCache CreateIVTable(IV16 iv, byte roundsExp)
        {
            Guard.NotNull(new object[] { iv });

            var ivCache  = new IVCache();
            var rounds   = 1u << roundsExp;
            var ivRounds = rounds;
            var ivTable  = new byte[rounds][];

            byte[] ivInput = iv.GetBytes();
            while (ivRounds > 0)
            {
                ivTable[ivTable.Length - ivRounds] = ivInput;

                ivInput = this._platform.ComputeSHA256(ivInput).Take(16).ToArray();

                ivRounds = ivRounds - 1;
            }
            ivCache.IVTable = new Tuple <byte[], byte[][]>(iv.GetBytes(), ivTable);
            return(ivCache);
        }
Beispiel #3
0
        public void AESEncryptMessageWithRandomKey(PaddedData paddedData, RandomKey32 randomKey, CipherV2 cipherV2, IVCache ivCache, LongRunningOperationContext context)
        {
            Guard.NotNull(new object[] { paddedData, randomKey, cipherV2, context });

            cipherV2.PlaintextPadding = paddedData.PlaintextPadding;

            if (cipherV2.RoundsExponent.Value == RoundsExponent.DontMakeRounds)
            {
                Debug.Assert(ivCache == null);
                cipherV2.MessageCipher = new MessageCipher(this._platform.ComputeAESRound(AESDir.Encrypt, cipherV2.IV16.GetBytes(), paddedData.GetBytes(), randomKey.GetBytes()));
            }
            else
            {
                context.EncryptionProgress.Message = LocalizableStrings.MsgEncryptingMessage;
                cipherV2.MessageCipher             = new MessageCipher(ComputeAESWithRounds(AESDir.Encrypt, cipherV2.IV16, paddedData.GetBytes(), randomKey.GetBytes(), cipherV2.RoundsExponent.Value, ivCache, context));
            }
        }
Beispiel #4
0
        public void AESEncryptRandomKeyWithInputDerivedKey(InputDerivedKey32 inputDerivedKey, RandomKey32 randomKey, CipherV2 cipherV2, IVCache ivCache, LongRunningOperationContext context)
        {
            Guard.NotNull(new object[] { inputDerivedKey, randomKey, cipherV2, context });

            if (cipherV2.RoundsExponent.Value == RoundsExponent.DontMakeRounds)
            {
                Debug.Assert(ivCache == null);
                cipherV2.RandomKeyCipher32 = new RandomKeyCipher32(this._platform.ComputeAESRound(AESDir.Encrypt, cipherV2.IV16.GetBytes(), randomKey.GetBytes(), inputDerivedKey.GetBytes()));
            }
            else
            {
                context.EncryptionProgress.Message = LocalizableStrings.MsgEncryptingRandomKey;
                cipherV2.RandomKeyCipher32         = new RandomKeyCipher32(ComputeAESWithRounds(AESDir.Encrypt, cipherV2.IV16, randomKey.GetBytes(), inputDerivedKey.GetBytes(), cipherV2.RoundsExponent.Value, ivCache, context));
            }
        }
Beispiel #5
0
        public PaddedData AESDecryptMessage(CipherV2 cipherV2, IV16 iv16, RandomKey32 randomKey, IVCache ivCache, LongRunningOperationContext context)
        {
            Guard.NotNull(new object[] { cipherV2, iv16, randomKey, context });

            byte[] paddedDataBytes;
            if (cipherV2.RoundsExponent.Value == RoundsExponent.DontMakeRounds)
            {
                paddedDataBytes = this._platform.ComputeAESRound(AESDir.Decrpyt, cipherV2.IV16.GetBytes(), cipherV2.MessageCipher.GetBytes(), randomKey.GetBytes());
            }
            else
            {
                context.EncryptionProgress.Message = LocalizableStrings.MsgDecryptingMessage;
                paddedDataBytes = ComputeAESWithRounds(AESDir.Decrpyt, cipherV2.IV16, cipherV2.MessageCipher.GetBytes(), randomKey.GetBytes(), cipherV2.RoundsExponent.Value, ivCache, context);
            }
            return(new PaddedData(paddedDataBytes, cipherV2.PlaintextPadding));
        }
Beispiel #6
0
        public RandomKey32 AESDecryptRandomKeyWithPasswordDerivedKey(CipherV2 cipherV2, InputDerivedKey32 inputDerivedKey, IVCache ivCache, LongRunningOperationContext context)
        {
            Guard.NotNull(new object[] { cipherV2, inputDerivedKey, context });

            byte[] randomKeyBytes;
            if (cipherV2.RoundsExponent.Value == RoundsExponent.DontMakeRounds)
            {
                randomKeyBytes = this._platform.ComputeAESRound(AESDir.Decrpyt, cipherV2.IV16.GetBytes(), cipherV2.RandomKeyCipher32.GetBytes(), inputDerivedKey.GetBytes());
            }
            else
            {
                context.EncryptionProgress.Message = LocalizableStrings.MsgDecryptingRandomKey;
                randomKeyBytes = ComputeAESWithRounds(AESDir.Decrpyt, cipherV2.IV16, cipherV2.RandomKeyCipher32.GetBytes(), inputDerivedKey.GetBytes(), cipherV2.RoundsExponent.Value, ivCache, context);
            }
            return(new RandomKey32(randomKeyBytes));
        }
Beispiel #7
0
        byte[] ComputeAESWithRounds(AESDir aesDir, IV16 iv, byte[] dataBytes, byte[] keyBytes, byte roundsExp, IVCache ivCache, LongRunningOperationContext context)
        {
            Guard.NotNull(new object[] { aesDir, iv, dataBytes, keyBytes, roundsExp, ivCache, context });
            var rounds     = 1u << roundsExp;
            var roundsToGo = rounds;

            byte[] inputData = dataBytes;
            byte[] aesResult = null;
            while (roundsToGo > 0)
            {
                byte[] currentIV =
                    aesDir == AESDir.Encrypt
                       ? ivCache.IVTable.Item2[roundsToGo - 1]
                       : ivCache.IVTable.Item2[ivCache.IVTable.Item2.Length - roundsToGo];

                aesResult = this._platform.ComputeAESRound(aesDir, currentIV, inputData, keyBytes);
                inputData = aesResult;

                roundsToGo--;                 // decrement before calculating the percentage or we'll be stuck at 99%

                // START encryptionProgress / Cancellation
                context.CancellationToken.ThrowIfCancellationRequested();
                decimal progressValue = (rounds - roundsToGo) / (decimal)(rounds);

                context.EncryptionProgress.Percent = (int)(progressValue * 100m);
                context.EncryptionProgress.Report(context.EncryptionProgress);
                // END encryptionProgress
            }
            return(aesResult);
        }
Beispiel #8
0
        public void AESEncryptMACWithRandomKey(CipherV2 cipherV2, MAC16 mac, RandomKey32 randomKey, IVCache ivCache, LongRunningOperationContext context)
        {
            Guard.NotNull(new object[] { cipherV2, mac, randomKey, context });

            if (cipherV2.RoundsExponent.Value == RoundsExponent.DontMakeRounds)
            {
                Debug.Assert(ivCache == null);
                cipherV2.MACCipher16 = new MACCipher16(this._platform.ComputeAESRound(AESDir.Encrypt, cipherV2.IV16.GetBytes(), mac.GetBytes(), randomKey.GetBytes()));
            }
            else
            {
                context.EncryptionProgress.Message = LocalizableStrings.MsgEncryptingMAC;
                cipherV2.MACCipher16 = new MACCipher16(ComputeAESWithRounds(AESDir.Encrypt, cipherV2.IV16, mac.GetBytes(), randomKey.GetBytes(), cipherV2.RoundsExponent.Value, ivCache, context));
            }
        }