raw
smg_comms_c_wrappers    1 //Wrapper methods for C implementations of RSA and MPI.
smg_comms_c_wrappers 2 //To be used / called from Ada so that the C part remains well separated and
smg_comms_c_wrappers 3 // can therefore be swapped easily at a later stage for something sane.
smg_comms_c_wrappers 4 //S.MG, 2018
smg_comms_c_wrappers 5
smg_comms_c_wrappers 6 #include "mpi.h"
smg_comms_c_wrappers 7
smg_comms_c_wrappers 8 //Comparison of 2 arrays of octets interpreted as MPIs.
smg_comms_c_wrappers 9 //This method creates 2 MPIs out of the given arrays of octes and then
smg_comms_c_wrappers 10 // calls the mpi_cmp method from mpi/mpi-cmp.c, returning its result.
smg_comms_c_wrappers 11 // ************************************************************************
smg_comms_c_wrappers 12 // ***NB: Make SURE that a and b have indeed allocated at least len_a and
smg_comms_c_wrappers 13 // ***** len_b octets respectively! NO CHECKS PERFORMED BY THIS METHOD!
smg_comms_c_wrappers 14 // ************************************************************************
smg_comms_c_wrappers 15 //@param a An array of octets representing the first MPI.
smg_comms_c_wrappers 16 //@param len_a The length of the first array (number of octets).
smg_comms_c_wrappers 17 //@param b An array of octets representing the second MPI.
smg_comms_c_wrappers 18 //@param len_b The length of the second array (number of octets).
smg_comms_c_wrappers 19 //@return 0 when a=b, -1 when a<b, 1 when a>b
smg_comms_c_wrappers 20 int mpi_cmp_octets(char *a, unsigned int len_a, char *b, unsigned int len_b);
smg_comms_c_wrappers 21
smg_comms_c_wrappers 22 //Encryption of given octets with public RSA key: n and e given as octets too.
smg_comms_c_wrappers 23 // ************************************************************************
smg_comms_c_wrappers 24 // ***Length of output is KEY_LENGTH_OCTETS.
smg_comms_c_wrappers 25 // ***NB: Make SURE that out, input, n and e have enough space allocated!!
smg_comms_c_wrappers 26 // ***NB: NO MEMORY ALLOCATED for its parameters by this method and
smg_comms_c_wrappers 27 // ***** NO CHECKS PERFORMED BY THIS METHOD!
smg_comms_c_wrappers 28 // ************************************************************************
smg_comms_c_wrappers 29 //@param out Pointer to ALREADY ALLOCATED space for the encrypted data.
smg_comms_c_wrappers 30 //@param len_out Length of the allocated space for out (in octets).
smg_comms_c_wrappers 31 //@param input Pointer to the data to be encrypted.
smg_comms_c_wrappers 32 //@param len_input Length of the allocated space for input (in octets).
smg_comms_c_wrappers 33 //@param n Pointer to the public RSA modulus to use for encryption.
smg_comms_c_wrappers 34 //@param len_n Length of n (in octets).
smg_comms_c_wrappers 35 //@param e Pointer to the public RSA exponent to use for encryption.
smg_comms_c_wrappers 36 //@param len_e Length of e (in octets).
smg_comms_c_wrappers 37 //@return The actual length of the output i.e. number of chars written to out.
smg_comms_c_wrappers 38 int public_rsa_octets( char *out , unsigned int len_out,
smg_comms_c_wrappers 39 char *input, unsigned int len_input,
smg_comms_c_wrappers 40 char *n , unsigned int len_n,
smg_comms_c_wrappers 41 char *e , unsigned int len_e);
smg_comms_c_wrappers 42
smg_comms_c_wrappers 43
smg_comms_c_wrappers 44 //Encryption of given octets with *private* RSA key given as octets.
smg_comms_c_wrappers 45 // ************************************************************************
smg_comms_c_wrappers 46 // ***Length of output is KEY_LENGTH_OCTETS.
smg_comms_c_wrappers 47 // ***NB: Make SURE that ALL pointers have enough space allocated!!
smg_comms_c_wrappers 48 // ***NB: NO MEMORY ALLOCATED for its parameters by this method and
smg_comms_c_wrappers 49 // ***** NO CHECKS PERFORMED BY THIS METHOD!
smg_comms_c_wrappers 50 // ************************************************************************
smg_comms_c_wrappers 51 //@param out Pointer to ALREADY ALLOCATED space for the encrypted data.
smg_comms_c_wrappers 52 //@param len_out Length of the allocated space for out (in octets).
smg_comms_c_wrappers 53 //@param input Pointer to the data to be encrypted.
smg_comms_c_wrappers 54 //@param len_input Length of the allocated space for input (in octets).
smg_comms_c_wrappers 55 //@param n Pointer to the public RSA modulus of the given key.
smg_comms_c_wrappers 56 //@param len_n Length of n (in octets).
smg_comms_c_wrappers 57 //@param e Pointer to the public RSA exponent of the given key.
smg_comms_c_wrappers 58 //@param len_e Length of e (in octets).
smg_comms_c_wrappers 59 //@param d Pointer to the private RSA exponent of the given key.
smg_comms_c_wrappers 60 //@param len_d Length of d (in octets).
smg_comms_c_wrappers 61 //@param p Pointer to the prime p of the given key.
smg_comms_c_wrappers 62 //@param len_p Length of p (in octets).
smg_comms_c_wrappers 63 //@param q Pointer to the prime q of the given key.
smg_comms_c_wrappers 64 //@param len_q Length of q (in octets).
smg_comms_c_wrappers 65 //@param u Pointer to the inverse of p mod q for the given key.
smg_comms_c_wrappers 66 //@param len_u Length of u (in octets).
smg_comms_c_wrappers 67 //@return The actual length of the output i.e. number of chars written to out.
smg_comms_c_wrappers 68 int private_rsa_octets( char *out, unsigned int len_out,
smg_comms_c_wrappers 69 char *input, unsigned int len_input,
smg_comms_c_wrappers 70 char *n , unsigned int len_n,
smg_comms_c_wrappers 71 char *e , unsigned int len_e,
smg_comms_c_wrappers 72 char *d , unsigned int len_d,
smg_comms_c_wrappers 73 char *p , unsigned int len_p,
smg_comms_c_wrappers 74 char *q , unsigned int len_q,
smg_comms_c_wrappers 75 char *u , unsigned int len_u);
smg_comms_c_wrappers 76
smg_comms_c_wrappers 77 //Generates a new RSA key and stores its components at the specified locations.
smg_comms_c_wrappers 78 //@param n Pointer to the public RSA modulus of the given key.
smg_comms_c_wrappers 79 //@param len_n Length of n (in octets).
smg_comms_c_wrappers 80 //@param e Pointer to the public RSA exponent of the given key.
smg_comms_c_wrappers 81 //@param len_e Length of e (in octets).
smg_comms_c_wrappers 82 //@param d Pointer to the private RSA exponent of the given key.
smg_comms_c_wrappers 83 //@param len_d Length of d (in octets).
smg_comms_c_wrappers 84 //@param p Pointer to the prime p of the given key.
smg_comms_c_wrappers 85 //@param len_p Length of p (in octets).
smg_comms_c_wrappers 86 //@param q Pointer to the prime q of the given key.
smg_comms_c_wrappers 87 //@param len_q Length of q (in octets).
smg_comms_c_wrappers 88 //@param u Pointer to the inverse of p mod q for the given key.
smg_comms_c_wrappers 89 //@param len_u Length of u (in octets).
smg_comms_c_wrappers 90 void gen_rsa_octets( char *n, unsigned int *len_n,
smg_comms_c_wrappers 91 char *e, unsigned int *len_e,
smg_comms_c_wrappers 92 char *d, unsigned int *len_d,
smg_comms_c_wrappers 93 char *p, unsigned int *len_p,
smg_comms_c_wrappers 94 char *q, unsigned int *len_q,
smg_comms_c_wrappers 95 char *u, unsigned int *len_u);
smg_comms_c_wrappers 96
smg_comms_c_wrappers 97 //Copies the buffer of m to the location to which out points.
smg_comms_c_wrappers 98 //*****************************************************************
smg_comms_c_wrappers 99 //*** This method does NOT allocate memory for out!
smg_comms_c_wrappers 100 //*** NB: caller should allocate enough memory!
smg_comms_c_wrappers 101 //*****************************************************************
smg_comms_c_wrappers 102 //@param out pointer to allocated memory where to copy the MPI's octets
smg_comms_c_wrappers 103 //@param len_out size of out; will be replaced by actual number of octets copied
smg_comms_c_wrappers 104 //@param m The MPI whose octets are to be retrieved
smg_comms_c_wrappers 105 void mpi_to_octets( char *out, unsigned int *len_out, MPI m);