/* smg_rsa.h * S.MG, 2017 */ #ifndef SMG_RSA_H #define SMG_RSA_H #include "mpi.h" #include "knobs.h" /* * These are constants as per TMSR RSA specification, NOT knobs! * TMSR key length is 4096 bits (512 octets); this means 2 primes of 2048 bits (256 octets) each. * NB: if you choose here an odd key length in octets you might end up with a smaller actual key, read the code. */ static const int KEY_LENGTH_OCTETS = 512; /*********truerandom.c*********/ /* * Opens and configures (as per FG requirements) the specified entropy source (e.g. "/dev/ttyUSB0") * @param source_name the name of the file to open (e.g. "/dev/ttyUSB0") * @return the descriptor of the open file when successful; negative value otherwise */ int open_entropy_source(char* source_name); /* * Returns noctets random octets (i.e. 8*noctets bits in total) as obtained from EuCrypt's preferred source. * Preferred source is defined in knobs.h as ENTROPY_SOURCE and should be a TRNG (e.g. Fuckgoats). * @param nboctets the length of desired random sequence, in octets * @param out pointer to allocated memory space for the requested random noctets; NB: this method does NOT allocate space! * @return the actual number of octets that were obtained from the currently configured entropy source (this is equal to noctets on successful read of required noctets) */ int get_random_octets(int noctets, unsigned char *out); /* Returns noctets random octets as obtained from the specified "from" source; * NB: the "from" source is considered to be the handle of an already opened stream; * This method will simply attempt to read from the source as needed! * * @param noctets the length of desired random sequence, in octets * @param out pointer to allocated memory space for the requested random octets; * NB: this method does NOT allocate space! * @param from handle of an already opened entropy source - this method will just READ from it as needed * @return the actual number of octets that were obtained */ int get_random_octets_from(int noctets, unsigned char *out, int from); /*********primegen.c*********/ /* * This is an implementation of the Miller-Rabin probabilistic primality test: * checking the specified number of randomly-chosen candidate witnesses * (i.e. with an outer bound of (1/4)^nwitnesses). * NB: a 1 result from this test means that the given n is indeed composite (non-prime) but a 0 result does not fully guarantee that n is prime! If this doesn't make sense to you, read more on probabilistic primality tests. * @param n the candidate prime number; the function will investigate whether this number is composite or *likely* to be prime. How likely? It depends on the number of witnesses checked, see next parameter. * @param nwitnesses this is the number of randomly chosen candidate witnesses to the compositeness of n that will be checked; the outer bound of the algorithm depends on this. * @param entropy_source the source of entropy (ready to read from) that will be used to choose candidate witnesses to the compositeness of n. * @return 1 if at least one witness to the compositeness of n has been found (i.e. n is certainly composite); 0 if no witness to the compositeness of n was found (i.e. it is likely that n is prime) * NB: the probability that n is *not* prime although this function returned 0 is less than (1/4)^nwitnesses, but it is NOT zero. */ int is_composite( MPI n, int nwitnesses, int entropy_source); /** * Generates a random number that has passed the Miller-Rabin test for primality (see function is_composite above). * NB: top 2 bits and bottom bit are ALWAYS 1! (i.e. a mask 110....01 is applied to the random bits) * a prime of 8*noctets long will have only (8*noctets-3) bits that are randomly chosen! * NB: this method does NOT allocate space for the requested MPI; it is the caller's responsibility to allocate it! * The source of randomness is ENTROPY_SOURCE in eucrypt/smg_rsa/include/knobs.h * The number of witnesses checked by Miller-Rabin is M_R_ITERATIONS in eucrypt/smg_rsa/include/knobs.h * Preconditions: * noctets > 0 (at least one octet!) * output has known allocated memory for at least nlimbs(noctets) * successful access to the entropy source * @param noctets the length of the desired prime number, in octets * @param output an MPI with sufficient memory allocated for a number that is noctets long */ void gen_random_prime( unsigned int noctets, MPI output); #endif /*SMG_RSA*/