(**************************************************************************)
(*                                                                        *)
(*  TLS implementation                                                    *)
(*  Copyright (C) 2025   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 TLSCrypt;

        (********************************************************)
        (*                                                      *)
        (*  Interface between TLS and the encryption algorithms *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            5 April 2018                    *)
        (*  Last edited:        24 May 2025                     *)
        (*  Status:             Apparently working              *)
        (*                                                      *)
        (********************************************************)


FROM SYSTEM IMPORT CARD8;

FROM TLSBase IMPORT
    (* type *)  TextType;

FROM VarStrings IMPORT
    (* type *)  ByteStringPtr;

FROM TransLog IMPORT
    (* type *)  TransactionLogID;


TYPE
    BulkCipherAlgorithm = (nocipher, rc4_128, tripledes_cbc, aes128_cbc, aes256_cbc);

    EncrRule =  RECORD
                    bulkcipher: BulkCipherAlgorithm;
                    key: ARRAY [0..31] OF CARD8;
                END (*RECORD*);

CONST MaxBlockSize = 16;


PROCEDURE InitEncryptionRule (VAR (*OUT*) ER: EncrRule);

    (* Creates an initial "no encryption" state. *)

PROCEDURE SetEncryption (VAR (*INOUT*) rule: EncrRule;  cipher: BulkCipherAlgorithm;
                            VAR (*IN*) key: ARRAY OF CARD8;  keylength: CARDINAL);

    (* Switches to a new encryption algorithm.  *)

PROCEDURE CloseEncryptionRule (VAR (*INOUT*) ER: EncrRule);

    (* Discards the EncrRule data. *)

PROCEDURE AppendCipherName (cipher: BulkCipherAlgorithm;
                                VAR (*INOUT*) message: ARRAY OF CHAR);

    (* Appends the name of the cipher to the message. *)

PROCEDURE KeyLength (cipher: BulkCipherAlgorithm): CARDINAL;

    (* Returns the key length in bytes. *)

PROCEDURE IVLength (cipher: BulkCipherAlgorithm): CARDINAL;

    (* Returns the initialisation vector length in bytes.  This is  *)
    (* for block ciphers only.                                      *)

PROCEDURE FixedIVLength (cipher: BulkCipherAlgorithm): CARDINAL;

    (* Returns the initialisation vector length in bytes.  This is  *)
    (* for AEAD ciphers only.                                       *)

PROCEDURE Encrypt (VAR (*IN*) rule: EncrRule;  logID: TransactionLogID;
                   VAR (*INOUT*) buffer: TextType;
                   VAR (*IN*) MAC: ARRAY OF CARD8;  maclength: CARDINAL);

    (* We encrypt the message in buffer after appending the MAC and     *)
    (* possibly some padding. The encrypted string replaces the         *)
    (* original message.                                                *)

PROCEDURE Decrypt (VAR (*IN*) rule: EncrRule;  logID: TransactionLogID;
                     VAR (*INOUT*) buffer: TextType;
                      VAR (*IN*) MAC: ARRAY OF CARD8;  maclength: CARDINAL): BOOLEAN;

    (* Decrypts an N-byte block of data, also returns the mac. *)

END TLSCrypt.

