sshkeygen.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * sshkeygen.h: routines used internally to key generation.
  3. */
  4. /* ----------------------------------------------------------------------
  5. * A table of all the primes that fit in a 16-bit integer. Call
  6. * init_primes_array to make sure it's been initialised.
  7. */
  8. #define NSMALLPRIMES 6542 /* number of primes < 65536 */
  9. extern const unsigned short *const smallprimes;
  10. void init_smallprimes(void);
  11. /* ----------------------------------------------------------------------
  12. * A system for making up random candidate integers during prime
  13. * generation. This unconditionally ensures that the numbers have the
  14. * right number of bits and are not divisible by any prime in the
  15. * smallprimes[] array above. It can also impose further constraints,
  16. * as documented below.
  17. */
  18. typedef struct PrimeCandidateSource PrimeCandidateSource;
  19. /*
  20. * pcs_new: you say how many bits you want the prime to have (with the
  21. * usual semantics that an n-bit number is in the range [2^{n-1},2^n))
  22. * and also optionally specify what you want its topmost 'nfirst' bits
  23. * to be.
  24. *
  25. * (The 'first' system is used for RSA keys, where you need to arrange
  26. * that the product of your two primes is in a more tightly
  27. * constrained range than the factor of 4 you'd get by just generating
  28. * two (n/2)-bit primes and multiplying them.)
  29. */
  30. PrimeCandidateSource *pcs_new(unsigned bits);
  31. PrimeCandidateSource *pcs_new_with_firstbits(unsigned bits,
  32. unsigned first, unsigned nfirst);
  33. /* Insist that generated numbers must be congruent to 'res' mod 'mod' */
  34. void pcs_require_residue(PrimeCandidateSource *s, mp_int *mod, mp_int *res);
  35. /* Convenience wrapper for the common case where res = 1 */
  36. void pcs_require_residue_1(PrimeCandidateSource *s, mp_int *mod);
  37. /* Same as pcs_require_residue_1, but also records that the modulus is
  38. * known to be prime */
  39. void pcs_require_residue_1_mod_prime(PrimeCandidateSource *s, mp_int *mod);
  40. /* Insist that generated numbers must _not_ be congruent to 'res' mod
  41. * 'mod'. This is used to avoid being 1 mod the RSA public exponent,
  42. * which is small, so it only needs ordinary integer parameters. */
  43. void pcs_avoid_residue_small(PrimeCandidateSource *s,
  44. unsigned mod, unsigned res);
  45. /* Exclude any prime that has no chance of being a Sophie Germain prime. */
  46. void pcs_try_sophie_germain(PrimeCandidateSource *s);
  47. /* Mark a PrimeCandidateSource as one-shot, so that the prime generation
  48. * function will return NULL if an attempt fails, rather than looping. */
  49. void pcs_set_oneshot(PrimeCandidateSource *s);
  50. /* Prepare a PrimeCandidateSource to actually generate numbers. This
  51. * function does last-minute computation that has to be delayed until
  52. * all constraints have been input. */
  53. void pcs_ready(PrimeCandidateSource *s);
  54. /* Actually generate a candidate integer. You must free the result, of
  55. * course. */
  56. mp_int *pcs_generate(PrimeCandidateSource *s);
  57. /* Free a PrimeCandidateSource. */
  58. void pcs_free(PrimeCandidateSource *s);
  59. /* Return some internal fields of the PCS. Used by testcrypt for
  60. * unit-testing this system. */
  61. void pcs_inspect(PrimeCandidateSource *pcs, mp_int **limit_out,
  62. mp_int **factor_out, mp_int **addend_out);
  63. /* Query functions for primegen to use */
  64. unsigned pcs_get_bits(PrimeCandidateSource *pcs);
  65. unsigned pcs_get_bits_remaining(PrimeCandidateSource *pcs);
  66. mp_int *pcs_get_upper_bound(PrimeCandidateSource *pcs);
  67. mp_int **pcs_get_known_prime_factors(PrimeCandidateSource *pcs, size_t *nout);
  68. /* ----------------------------------------------------------------------
  69. * A system for doing Miller-Rabin probabilistic primality tests.
  70. * These benefit from having set up some context beforehand, if you're
  71. * going to do more than one of them on the same candidate prime, so
  72. * we declare an object type here to store that context.
  73. */
  74. typedef struct MillerRabin MillerRabin;
  75. /* Make and free a Miller-Rabin context. */
  76. MillerRabin *miller_rabin_new(mp_int *p);
  77. void miller_rabin_free(MillerRabin *mr);
  78. /* Perform a single Miller-Rabin test, using a random witness value. */
  79. bool miller_rabin_test_random(MillerRabin *mr);
  80. /* Suggest how many tests are needed to make it sufficiently unlikely
  81. * that a composite number will pass them all */
  82. unsigned miller_rabin_checks_needed(unsigned bits);
  83. /* An extension to the M-R test, which iterates until it either finds
  84. * a witness value that is potentially a primitive root, or one
  85. * that proves the number to be composite. */
  86. mp_int *miller_rabin_find_potential_primitive_root(MillerRabin *mr);
  87. /* ----------------------------------------------------------------------
  88. * A system for proving numbers to be prime, using the Pocklington
  89. * test, which requires knowing a partial factorisation of p-1
  90. * (specifically, factors whose product is at least cbrt(p)) and a
  91. * primitive root.
  92. *
  93. * The API consists of instantiating a 'Pockle' object, which
  94. * internally stores a list of numbers you've already convinced it is
  95. * prime, and can accept further primes if you give a satisfactory
  96. * certificate of their primality based on primes it already knows
  97. * about.
  98. */
  99. typedef struct Pockle Pockle;
  100. /* In real use, you only really need to know whether the Pockle
  101. * successfully accepted your prime. But for testcrypt, it's useful to
  102. * expose many different failure modes so we can try to provoke them
  103. * all in unit tests and check they're working. */
  104. #define POCKLE_STATUSES(X) \
  105. X(POCKLE_OK) \
  106. X(POCKLE_SMALL_PRIME_NOT_SMALL) \
  107. X(POCKLE_SMALL_PRIME_NOT_PRIME) \
  108. X(POCKLE_PRIME_SMALLER_THAN_2) \
  109. X(POCKLE_FACTOR_NOT_KNOWN_PRIME) \
  110. X(POCKLE_FACTOR_NOT_A_FACTOR) \
  111. X(POCKLE_PRODUCT_OF_FACTORS_TOO_SMALL) \
  112. X(POCKLE_FERMAT_TEST_FAILED) \
  113. X(POCKLE_DISCRIMINANT_IS_SQUARE) \
  114. X(POCKLE_WITNESS_POWER_IS_1) \
  115. X(POCKLE_WITNESS_POWER_NOT_COPRIME) \
  116. /* end of list */
  117. #define DEFINE_ENUM(id) id,
  118. typedef enum PockleStatus { POCKLE_STATUSES(DEFINE_ENUM) } PockleStatus;
  119. #undef DEFINE_ENUM
  120. /* Make a new empty Pockle, containing no primes. */
  121. Pockle *pockle_new(void);
  122. /* Insert a prime below 2^32 into the Pockle. No evidence is required:
  123. * Pockle will check it itself. */
  124. PockleStatus pockle_add_small_prime(Pockle *pockle, mp_int *p);
  125. /* Insert a general prime into the Pockle. You must provide a list of
  126. * prime factors of p-1, whose product exceeds the cube root of p, and
  127. * also a primitive root mod p. */
  128. PockleStatus pockle_add_prime(Pockle *pockle, mp_int *p,
  129. mp_int **factors, size_t nfactors,
  130. mp_int *primitive_root);
  131. /* If you call pockle_mark, and later pass the returned value to
  132. * pockle_release, it will free all the primes that were added to the
  133. * Pockle between those two calls. Useful in recursive algorithms, to
  134. * stop the Pockle growing unboundedly if the recursion keeps having
  135. * to backtrack. */
  136. size_t pockle_mark(Pockle *pockle);
  137. void pockle_release(Pockle *pockle, size_t mark);
  138. /* Free a Pockle. */
  139. void pockle_free(Pockle *pockle);
  140. /* Generate a certificate of primality for a prime already known to
  141. * the Pockle, in a format acceptable to Math::Prime::Util. */
  142. strbuf *pockle_mpu(Pockle *pockle, mp_int *p);
  143. /* ----------------------------------------------------------------------
  144. * Callback API that allows key generation to report progress to its
  145. * caller.
  146. */
  147. typedef struct ProgressReceiverVtable ProgressReceiverVtable;
  148. typedef struct ProgressReceiver ProgressReceiver;
  149. typedef union ProgressPhase ProgressPhase;
  150. union ProgressPhase {
  151. int n;
  152. void *p;
  153. };
  154. struct ProgressReceiver {
  155. const ProgressReceiverVtable *vt;
  156. };
  157. struct ProgressReceiverVtable {
  158. ProgressPhase (*add_linear)(ProgressReceiver *prog, double overall_cost);
  159. ProgressPhase (*add_probabilistic)(ProgressReceiver *prog,
  160. double cost_per_attempt,
  161. double attempt_probability);
  162. void (*ready)(ProgressReceiver *prog);
  163. void (*start_phase)(ProgressReceiver *prog, ProgressPhase phase);
  164. void (*report)(ProgressReceiver *prog, double progress);
  165. void (*report_attempt)(ProgressReceiver *prog);
  166. void (*report_phase_complete)(ProgressReceiver *prog);
  167. };
  168. static inline ProgressPhase progress_add_linear(ProgressReceiver *prog,
  169. double c)
  170. { return prog->vt->add_linear(prog, c); }
  171. static inline ProgressPhase progress_add_probabilistic(ProgressReceiver *prog,
  172. double c, double p)
  173. { return prog->vt->add_probabilistic(prog, c, p); }
  174. static inline void progress_ready(ProgressReceiver *prog)
  175. { prog->vt->ready(prog); }
  176. static inline void progress_start_phase(
  177. ProgressReceiver *prog, ProgressPhase phase)
  178. { prog->vt->start_phase(prog, phase); }
  179. static inline void progress_report(ProgressReceiver *prog, double progress)
  180. { prog->vt->report(prog, progress); }
  181. static inline void progress_report_attempt(ProgressReceiver *prog)
  182. { prog->vt->report_attempt(prog); }
  183. static inline void progress_report_phase_complete(ProgressReceiver *prog)
  184. { prog->vt->report_phase_complete(prog); }
  185. ProgressPhase null_progress_add_linear(
  186. ProgressReceiver *prog, double c);
  187. ProgressPhase null_progress_add_probabilistic(
  188. ProgressReceiver *prog, double c, double p);
  189. void null_progress_ready(ProgressReceiver *prog);
  190. void null_progress_start_phase(ProgressReceiver *prog, ProgressPhase phase);
  191. void null_progress_report(ProgressReceiver *prog, double progress);
  192. void null_progress_report_attempt(ProgressReceiver *prog);
  193. void null_progress_report_phase_complete(ProgressReceiver *prog);
  194. extern const ProgressReceiverVtable null_progress_vt;
  195. /* A helper function for dreaming up progress cost estimates. */
  196. double estimate_modexp_cost(unsigned bits);
  197. /* ----------------------------------------------------------------------
  198. * The top-level API for generating primes.
  199. */
  200. typedef struct PrimeGenerationPolicy PrimeGenerationPolicy;
  201. typedef struct PrimeGenerationContext PrimeGenerationContext;
  202. struct PrimeGenerationContext {
  203. const PrimeGenerationPolicy *vt;
  204. };
  205. struct PrimeGenerationPolicy {
  206. ProgressPhase (*add_progress_phase)(const PrimeGenerationPolicy *policy,
  207. ProgressReceiver *prog, unsigned bits);
  208. PrimeGenerationContext *(*new_context)(
  209. const PrimeGenerationPolicy *policy);
  210. void (*free_context)(PrimeGenerationContext *ctx);
  211. mp_int *(*generate)(
  212. PrimeGenerationContext *ctx,
  213. PrimeCandidateSource *pcs, ProgressReceiver *prog);
  214. strbuf *(*mpu_certificate)(PrimeGenerationContext *ctx, mp_int *p);
  215. const void *extra; /* additional data a particular impl might need */
  216. };
  217. static inline ProgressPhase primegen_add_progress_phase(
  218. PrimeGenerationContext *ctx, ProgressReceiver *prog, unsigned bits)
  219. { return ctx->vt->add_progress_phase(ctx->vt, prog, bits); }
  220. static inline PrimeGenerationContext *primegen_new_context(
  221. const PrimeGenerationPolicy *policy)
  222. { return policy->new_context(policy); }
  223. static inline void primegen_free_context(PrimeGenerationContext *ctx)
  224. { ctx->vt->free_context(ctx); }
  225. static inline mp_int *primegen_generate(
  226. PrimeGenerationContext *ctx,
  227. PrimeCandidateSource *pcs, ProgressReceiver *prog)
  228. { return ctx->vt->generate(ctx, pcs, prog); }
  229. static inline strbuf *primegen_mpu_certificate(
  230. PrimeGenerationContext *ctx, mp_int *p)
  231. { return ctx->vt->mpu_certificate(ctx, p); }
  232. extern const PrimeGenerationPolicy primegen_probabilistic;
  233. extern const PrimeGenerationPolicy primegen_provable_fast;
  234. extern const PrimeGenerationPolicy primegen_provable_maurer_simple;
  235. extern const PrimeGenerationPolicy primegen_provable_maurer_complex;
  236. /* ----------------------------------------------------------------------
  237. * The overall top-level API for generating entire key pairs.
  238. */
  239. int rsa_generate(RSAKey *key, int bits, bool strong,
  240. PrimeGenerationContext *pgc, ProgressReceiver *prog);
  241. int dsa_generate(struct dss_key *key, int bits, PrimeGenerationContext *pgc,
  242. ProgressReceiver *prog);
  243. int ecdsa_generate(struct ecdsa_key *key, int bits);
  244. int eddsa_generate(struct eddsa_key *key, int bits);