//Wrapper methods for C implementations of RSA and MPI. //To be used / called from Ada so that the C part remains well separated and // can therefore be swapped easily at a later stage for something sane. //S.MG, 2018 #include "mpi.h" //Comparison of 2 arrays of octets interpreted as MPIs. //This method creates 2 MPIs out of the given arrays of octes and then // calls the mpi_cmp method from mpi/mpi-cmp.c, returning its result. // ************************************************************************ // ***NB: Make SURE that a and b have indeed allocated at least len_a and // ***** len_b octets respectively! NO CHECKS PERFORMED BY THIS METHOD! // ************************************************************************ //@param a An array of octets representing the first MPI. //@param len_a The length of the first array (number of octets). //@param b An array of octets representing the second MPI. //@param len_b The length of the second array (number of octets). //@return 0 when a=b, -1 when ab int mpi_cmp_octets(char *a, unsigned int len_a, char *b, unsigned int len_b); //Encryption of given octets with public RSA key: n and e given as octets too. // ************************************************************************ // ***Length of output is KEY_LENGTH_OCTETS. // ***NB: Make SURE that out, input, n and e have enough space allocated!! // ***NB: NO MEMORY ALLOCATED for its parameters by this method and // ***** NO CHECKS PERFORMED BY THIS METHOD! // ************************************************************************ //@param out Pointer to ALREADY ALLOCATED space for the encrypted data. //@param len_out Length of the allocated space for out (in octets). //@param input Pointer to the data to be encrypted. //@param len_input Length of the allocated space for input (in octets). //@param n Pointer to the public RSA modulus to use for encryption. //@param len_n Length of n (in octets). //@param e Pointer to the public RSA exponent to use for encryption. //@param len_e Length of e (in octets). //@return The actual length of the output i.e. number of chars written to out. int public_rsa_octets( char *out , unsigned int len_out, char *input, unsigned int len_input, char *n , unsigned int len_n, char *e , unsigned int len_e); //Encryption of given octets with *private* RSA key given as octets. // ************************************************************************ // ***Length of output is KEY_LENGTH_OCTETS. // ***NB: Make SURE that ALL pointers have enough space allocated!! // ***NB: NO MEMORY ALLOCATED for its parameters by this method and // ***** NO CHECKS PERFORMED BY THIS METHOD! // ************************************************************************ //@param out Pointer to ALREADY ALLOCATED space for the encrypted data. //@param len_out Length of the allocated space for out (in octets). //@param input Pointer to the data to be encrypted. //@param len_input Length of the allocated space for input (in octets). //@param n Pointer to the public RSA modulus of the given key. //@param len_n Length of n (in octets). //@param e Pointer to the public RSA exponent of the given key. //@param len_e Length of e (in octets). //@param d Pointer to the private RSA exponent of the given key. //@param len_d Length of d (in octets). //@param p Pointer to the prime p of the given key. //@param len_p Length of p (in octets). //@param q Pointer to the prime q of the given key. //@param len_q Length of q (in octets). //@param u Pointer to the inverse of p mod q for the given key. //@param len_u Length of u (in octets). //@return The actual length of the output i.e. number of chars written to out. int private_rsa_octets( char *out, unsigned int len_out, char *input, unsigned int len_input, char *n , unsigned int len_n, char *e , unsigned int len_e, char *d , unsigned int len_d, char *p , unsigned int len_p, char *q , unsigned int len_q, char *u , unsigned int len_u); //Generates a new RSA key and stores its components at the specified locations. //@param n Pointer to the public RSA modulus of the given key. //@param len_n Length of n (in octets). //@param e Pointer to the public RSA exponent of the given key. //@param len_e Length of e (in octets). //@param d Pointer to the private RSA exponent of the given key. //@param len_d Length of d (in octets). //@param p Pointer to the prime p of the given key. //@param len_p Length of p (in octets). //@param q Pointer to the prime q of the given key. //@param len_q Length of q (in octets). //@param u Pointer to the inverse of p mod q for the given key. //@param len_u Length of u (in octets). void gen_rsa_octets( char *n, unsigned int *len_n, char *e, unsigned int *len_e, char *d, unsigned int *len_d, char *p, unsigned int *len_p, char *q, unsigned int *len_q, char *u, unsigned int *len_u); //Copies the buffer of m to the location to which out points. //***************************************************************** //*** This method does NOT allocate memory for out! //*** NB: caller should allocate enough memory! //***************************************************************** //@param out pointer to allocated memory where to copy the MPI's octets //@param len_out size of out; will be replaced by actual number of octets copied //@param m The MPI whose octets are to be retrieved void mpi_to_octets( char *out, unsigned int *len_out, MPI m);