(**************************************************************************)
(*                                                                        *)
(*  Weasel mail server                                                    *)
(*  Copyright (C) 2024   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 PassEncrypt;

        (********************************************************)
        (*                                                      *)
        (*           Password encryption/decryption             *)
        (*                                                      *)
        (*   Several algorithms are supported because I have    *)
        (*           not yet decided which one to use           *)
        (*                                                      *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            12 March 2024                   *)
        (*  Last edited:        14 March 2024                   *)
        (*  Status:             Working                         *)
        (*                                                      *)
        (*  This is a specialised version of encryption, where  *)
        (*  the password is expected to be up to 32 characters  *)
        (*  long, giving an encrypted result which, after hex   *)
        (*  encoding, is 64 characters long.                    *)
        (*                                                      *)
        (********************************************************)


FROM SYSTEM IMPORT CARD8;

TYPE algorithm = (rc4, des, des3, aes128, aes192, aes256);

TYPE
    KLtype = ARRAY algorithm OF CARDINAL;
    AlgNameType = ARRAY algorithm OF ARRAY [0..7] OF CHAR;

CONST
    keylength = KLtype {16, 8, 24, 16, 24, 32};
    algName = AlgNameType {"RC4", "DES", "3DES", "AES128", "AES192", "AES256"};


PROCEDURE SetAlgorithm (newalg: algorithm);

    (* Specifies the algorithm that will henceforth be used. *)

PROCEDURE EncryptPassword (VAR pwd, encrypted: ARRAY OF CHAR);

    (* Encrypt a password.  The result is hex-encoded, therefore    *)
    (* twice the maximum password size.                             *)

PROCEDURE DecryptPassword (VAR encrypted, pwd: ARRAY OF CHAR);

    (* Decrypt a hex-encoded encrypted password. *)

END PassEncrypt.

