/// <summary> /// Process the last block of data /// </summary> /// /// <param name="Output">The hash value return</param> /// <param name="OutOffset">The offset in the data</param> /// /// <returns>The number of bytes processed</returns> /// /// <exception cref="CryptoMacException">Thrown if Output array is too small</exception> public int DoFinal(byte[] Output, int OutOffset) { if (Output.Length - OutOffset < m_macSize) { throw new CryptoMacException("CMAC:DoFinal", "The Output buffer is too short!", new ArgumentOutOfRangeException()); } if (m_wrkOffset != m_blockSize) { ISO7816 pad = new ISO7816(); pad.AddPadding(m_wrkBuffer, m_wrkOffset); for (int i = 0; i < m_msgCode.Length; i++) { m_wrkBuffer[i] ^= m_K2[i]; } } else { for (int i = 0; i < m_msgCode.Length; i++) { m_wrkBuffer[i] ^= m_K1[i]; } } m_cipherMode.Transform(m_wrkBuffer, 0, m_msgCode, 0); Buffer.BlockCopy(m_msgCode, 0, Output, OutOffset, m_macSize); Reset(); return(m_macSize); }
private void OfbModeTest() { AllocateRandom(ref _iv, 16); AllocateRandom(ref _key, 32); KeyParams kp = new KeyParams(_key, _iv); RHX eng = new RHX(); OFB cipher = new OFB(eng); OFB cipher2 = new OFB(eng); ISO7816 padding = new ISO7816(); cipher.IsParallel = false; CipherStream cs = new CipherStream(cipher2, padding); for (int i = 0; i < 10; i++) { int sze = AllocateRandom(ref _plnText, 0, eng.BlockSize); int prlBlock = sze - (sze % (cipher.BlockSize * _processorCount)); _cmpText = new byte[sze]; _decText = new byte[sze]; _encText = new byte[sze]; MemoryStream mIn = new MemoryStream(_plnText); MemoryStream mOut = new MemoryStream(); MemoryStream mRes = new MemoryStream(); // *** Compare encryption output *** // // local processor cipher.Initialize(true, kp); BlockEncrypt(cipher, padding, _plnText, 0, ref _encText, 0); // streamcipher linear mode cs.IsParallel = false; // memorystream interface cs.Initialize(true, kp); cs.Write(mIn, mOut); if (!Evaluate.AreEqual(mOut.ToArray(), _encText)) { throw new Exception("CipherStreamTest: Encrypted arrays are not equal!"); } // byte array interface cs.Initialize(true, kp); cs.Write(_plnText, 0, ref _cmpText, 0); if (!Evaluate.AreEqual(_cmpText, _encText)) { throw new Exception("CipherStreamTest: Encrypted arrays are not equal!"); } // ***compare decryption output *** // // local processor cipher.Initialize(false, kp); BlockDecrypt(cipher, padding, _encText, 0, ref _decText, 0); if (!Evaluate.AreEqual(_plnText, _decText)) { throw new Exception("CipherStreamTest: Decrypted arrays are not equal!"); } // decrypt linear mode cipher2.IsParallel = false; mOut.Seek(0, SeekOrigin.Begin); cs.Initialize(false, kp); cs.Write(mOut, mRes); if (!Evaluate.AreEqual(mRes.ToArray(), _decText)) { throw new Exception("CipherStreamTest: Decrypted arrays are not equal!"); } // byte array interface cs.Initialize(false, kp); Array.Resize(ref _cmpText, _encText.Length); cs.Write(_encText, 0, ref _cmpText, 0); if (!Evaluate.AreEqual(_cmpText, _decText)) { throw new Exception("CipherStreamTest: Decrypted arrays are not equal!"); } } eng.Dispose(); }