DEFINITION MODULE WRSA;

        (********************************************************)
        (*                                                      *)
        (*                     RSA signing                      *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            8 August 2023                   *)
        (*  Last edited:        10 May 2024                     *)
        (*  Status:             OK                              *)
        (*                                                      *)
        (*  This is an adaptation of the sshRSA module in       *)
        (*  my SSH implementation, retaining only those         *)
        (*  features needed for DKIM calculations.              *)
        (*                                                      *)
        (********************************************************)


IMPORT HashAlgs, RSAKeys;

FROM SYSTEM IMPORT CARD8;
FROM VarStrings IMPORT ByteStr;
FROM TransLog IMPORT TransactionLogID;


PROCEDURE LoadRSAPrivateKey (filename: ARRAY OF CHAR): RSAKeys.RSAKeyType;

    (* Gets an RSA private key from a file.  Returns a nil key on error. *)

PROCEDURE LoadPublicKey (VAR (*IN*) bindata: ARRAY OF CARD8): RSAKeys.RSAKeyType;

    (* Converts a binary string, in the format required by DKIM, to *)
    (* an RSA public key.                                           *)

PROCEDURE Sign (privatekey: RSAKeys.RSAKeyType;  hashf: HashAlgs.HashAlgorithm;
                              VAR (*IN*) digest: ARRAY OF CARD8): ByteStr;

    (* Creates an RSA signature using the specified hash function and   *)
    (* our private key.                                                 *)

PROCEDURE CheckSignature (hashf: HashAlgs.HashAlgorithm;
                            VAR (*IN*) digest: ARRAY OF CARD8;
                              publickey: RSAKeys.RSAKeyType;
                               VAR (*INOUT*) signature: ByteStr;
                                logID: TransactionLogID): BOOLEAN;

    (* Checks that signature is a valid signature of the digest,        *)
    (* using the given public key.  The verification method is          *)
    (* specified in RFC 8017 section 8.2.2 (page 37).                   *)
    (* During processing the signature is discarded.                    *)

END WRSA.

