DEFINITION MODULE RSA;

        (********************************************************)
        (*                                                      *)
        (*                    RSA encryption                    *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Last edited:        22 September 2023               *)
        (*  Status:             Working                         *)
        (*                                                      *)
        (*      In case you're wondering what RSA stands for,   *)
        (*      it's Rivest-Shamir-Adleman, after the           *)
        (*      inventors of the algorithm.                     *)
        (*                                                      *)
        (********************************************************)


FROM BigNum IMPORT (*type*) BN;
FROM RSAKeys IMPORT (*type*) RSAKeyType;


(************************************************************************)
(* Procedures PubKeyEncode and PrivKeyEncode perform the same function  *)
(* as Encrypt and Decrypt, respectively.  My reason for introducing     *)
(* them is that the old names are misleading.  A message can be         *)
(* encrypted with a private key and then decrypted with a public key.   *)
(* Alternatively, depending on the application, it can be encrypted     *)
(* with a public key and then decrypted with a private key.  That means *)
(* that the distinction between encryption and decryption is not basic. *)
(* The important distinction is between public key and private key.     *)
(* In the long run I want to phase out procedures Encrypt and Decrypt,  *)
(* and use only the new names.                                          *)
(************************************************************************)

PROCEDURE PubKeyEncode (m: BN;  key: RSAKeyType): BN;    (* key is PUBLIC key *)

    (* m is a numerical encoding of the message, with 0 <= M < key.n    *)
    (* We produce a ciphertext using only the public part of the key.   *)

PROCEDURE Encrypt (m: BN;  key: RSAKeyType): BN;    (* key is PUBLIC key *)

    (* m is a numerical encoding of the message, with 0 <= M < key.n    *)
    (* We produce a ciphertext using only the public part of the key.   *)

PROCEDURE SlowDecrypt (c: BN;  key: RSAKeyType): BN; (* key is PRIVATE key *)

    (* c is the ciphertext.  We decode it using the private key.  This  *)
    (* is the version to use if we know the private key but not its     *)
    (* (p, q, dp, dq, qinv) components.                                 *)

PROCEDURE PrivKeyEncode (m: BN;  key: RSAKeyType): BN;    (* key is PRIVATE key *)

    (* c is the ciphertext.  We decode it using the private key.  This  *)
    (* operation is equivalent to                                       *)
    (*          BigNum.ModularPower (c, key.private, key.n)             *)
    (* but we gain some speed by working with smaller numbers.          *)

PROCEDURE Decrypt (c: BN;  key: RSAKeyType): BN;  (* key is PRIVATE key *)

    (* c is the ciphertext.  We decode it using the private key.  This  *)
    (* operation is equivalent to                                       *)
    (*          BigNum.ModularPower (c, key.private, key.n)             *)
    (* but we gain some speed by working with smaller numbers.          *)

END RSA.

