// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Mobius Forensic Toolkit
// Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022 Eduardo Aguiar
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include "cipher_rc4.h"
#include <mobius/exception.inc>
#include <stdexcept>

namespace mobius
{
namespace crypt
{
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief Constructor
//! \param key encryption/decryption key
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
cipher_rc4::cipher_rc4 (const bytearray& key)
{
  if (key.empty () || key.size () > 255)
    throw std::out_of_range (MOBIUS_EXCEPTION_MSG ("Invalid key size"));

  for (std::size_t i = 0; i < 256; i++)
    start_s_[i] = i;

  std::uint8_t j = 0;
  std::uint8_t tmp;

  for (std::size_t i = 0; i < 256; i++)
    {
      j += start_s_[i] + key[i % key.size ()];
      tmp = start_s_[i];
      start_s_[i] = start_s_[j];
      start_s_[j] = tmp;
    }

  std::copy (std::begin (start_s_), std::end (start_s_), std::begin (S_));
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief Reset state
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void
cipher_rc4::reset ()
{
  i_ = j_ = 0;
  std::copy (std::begin (start_s_), std::end (start_s_), std::begin (S_));
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief Encrypt/decrypt one byte
//! \param c byte
//! \return a new byte
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
std::uint8_t
cipher_rc4::_crypt_byte (std::uint8_t c) noexcept
{
  i_++;
  j_ += S_[i_];

  std::uint8_t tmp = S_[i_];
  S_[i_] = S_[j_];
  S_[j_] = tmp;

  std::uint8_t k = S_[(S_[i_] + S_[j_]) & 0xff];

  return c ^ k;
}

} // namespace crypt
} // namespace mobius
