DEFINITION MODULE TLSHMAC;

        (********************************************************)
        (*                                                      *)
        (*              Message authentication code             *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            16 January 2021                 *)
        (*  Last edited:        13 June 2025                    *)
        (*  Status:             Working                         *)
        (*                                                      *)
        (********************************************************)


FROM SYSTEM IMPORT CARD8;
FROM VarStrings IMPORT ByteStringPtr;


TYPE HMACType = (nullhmac, hmacmd5, hmacsha1, hmacsha1_96, hmacsha256, hmacsha256_96,
                 hmacsha384, hmacsha384_96, hmacsha512, hmacsha512_96,
                 hmacmd5_96, none);

CONST MaxHMACSize = 64;


PROCEDURE AppendHMACName (mt: HMACType;  VAR (*INOUT*) message: ARRAY OF CHAR);

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

PROCEDURE HMAClength (mt: HMACType): CARDINAL;

    (* Returns the number of bytes in the HMAC. *)

PROCEDURE HMACkeylength (mt: HMACType): CARDINAL;

    (* Returns the number of bytes in the HMAC key.  For all HMAC       *)
    (* algorithms listed in RFC 5246, the key length is equal to the    *)
    (* HMAC length.                                                     *)

PROCEDURE ComputeHMac (mt: HMACType;  VAR (*IN*) key: ARRAY OF CARD8;
                       keylength: CARDINAL;
                        dataptr: ByteStringPtr;  datalength: CARDINAL;
                         VAR (*OUT*) outdata: ARRAY OF CARD8;
                          resultlength: CARDINAL);

    (* The HMAC is outdata, of size resultlength bytes.  The "normal"   *)
    (* can be found by calling HMAClength, but a smaller length can be  *)
    (* specified if a truncated HMAC is desired.                        *)
    (* The input is an unencrypted byte string.                         *)

END TLSHMAC.

