(**************************************************************************)
(*                                                                        *)
(*  Encryption library                                                    *)
(*  Copyright (C) 2018   Peter Moylan                                     *)
(*                                                                        *)
(*  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 3 of the License, 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/>. *)
(*                                                                        *)
(*  To contact author:   http://www.pmoylan.org   peter@pmoylan.org       *)
(*                                                                        *)
(**************************************************************************)

DEFINITION MODULE RC4;

        (********************************************************)
        (*                                                      *)
        (*                  STREAM CIPHER RC4                   *)
        (*                                                      *)
        (*   This is now deprecated as an encryption method,    *)
        (*   because of known attacks, but it is included in    *)
        (*          this package for completeness.              *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            17 April 2018                   *)
        (*  Last edited:        18 April 2018                   *)
        (*  Status:             Passes all tests                *)
        (*                                                      *)
        (********************************************************)

(************************************************************************)
(*                                                                      *)
(* The algorithm used in this module is based on the information in     *)
(*              https://en.wikipedia.org/wiki/RC4                       *)
(*                                                                      *)
(************************************************************************)


FROM SYSTEM IMPORT LOC, CARD8;

TYPE
    RC4state;           (* is private *)

PROCEDURE RC4Init (offset: CARDINAL;  key: ARRAY OF LOC;  keylength: CARDINAL)
                                                                       : RC4state;

    (* Starts a new encryption or decryption operation.  The offset is the  *)
    (* initial number of bytes we discard.                                  *)

PROCEDURE Encrypt (state: RC4state;  length: CARDINAL;
                                        VAR (*IN*) input: ARRAY OF LOC;
                                        VAR (*OUT*) output: ARRAY OF LOC);

    (* Transforms input to output.  This same procedure is used either for  *)
    (* encryption or decryption.                                            *)

PROCEDURE RC4Close (VAR (*INOUT*) state: RC4state);

    (* Terminates the operation with this state. *)

END RC4.

