sshkeygen.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 specified witness value.
  79. * Used in the test suite. */
  80. struct mr_result {
  81. unsigned passed;
  82. unsigned potential_primitive_root;
  83. };
  84. struct mr_result miller_rabin_test(MillerRabin *mr, mp_int *w);
  85. /* Perform a single Miller-Rabin test, using a random witness value. */
  86. bool miller_rabin_test_random(MillerRabin *mr);
  87. /* Suggest how many tests are needed to make it sufficiently unlikely
  88. * that a composite number will pass them all */
  89. unsigned miller_rabin_checks_needed(unsigned bits);
  90. /* An extension to the M-R test, which iterates until it either finds
  91. * a witness value that is potentially a primitive root, or one
  92. * that proves the number to be composite. */
  93. mp_int *miller_rabin_find_potential_primitive_root(MillerRabin *mr);
  94. /* ----------------------------------------------------------------------
  95. * A system for proving numbers to be prime, using the Pocklington
  96. * test, which requires knowing a partial factorisation of p-1
  97. * (specifically, factors whose product is at least cbrt(p)) and a
  98. * primitive root.
  99. *
  100. * The API consists of instantiating a 'Pockle' object, which
  101. * internally stores a list of numbers you've already convinced it is
  102. * prime, and can accept further primes if you give a satisfactory
  103. * certificate of their primality based on primes it already knows
  104. * about.
  105. */
  106. typedef struct Pockle Pockle;
  107. /* In real use, you only really need to know whether the Pockle
  108. * successfully accepted your prime. But for testcrypt, it's useful to
  109. * expose many different failure modes so we can try to provoke them
  110. * all in unit tests and check they're working. */
  111. #define POCKLE_STATUSES(X) \
  112. X(POCKLE_OK) \
  113. X(POCKLE_SMALL_PRIME_NOT_SMALL) \
  114. X(POCKLE_SMALL_PRIME_NOT_PRIME) \
  115. X(POCKLE_PRIME_SMALLER_THAN_2) \
  116. X(POCKLE_FACTOR_NOT_KNOWN_PRIME) \
  117. X(POCKLE_FACTOR_NOT_A_FACTOR) \
  118. X(POCKLE_PRODUCT_OF_FACTORS_TOO_SMALL) \
  119. X(POCKLE_FERMAT_TEST_FAILED) \
  120. X(POCKLE_DISCRIMINANT_IS_SQUARE) \
  121. X(POCKLE_WITNESS_POWER_IS_1) \
  122. X(POCKLE_WITNESS_POWER_NOT_COPRIME) \
  123. /* end of list */
  124. #define DEFINE_ENUM(id) id,
  125. typedef enum PockleStatus { POCKLE_STATUSES(DEFINE_ENUM) } PockleStatus;
  126. #undef DEFINE_ENUM
  127. /* Make a new empty Pockle, containing no primes. */
  128. Pockle *pockle_new(void);
  129. /* Insert a prime below 2^32 into the Pockle. No evidence is required:
  130. * Pockle will check it itself. */
  131. PockleStatus pockle_add_small_prime(Pockle *pockle, mp_int *p);
  132. /* Insert a general prime into the Pockle. You must provide a list of
  133. * prime factors of p-1, whose product exceeds the cube root of p, and
  134. * also a primitive root mod p. */
  135. PockleStatus pockle_add_prime(Pockle *pockle, mp_int *p,
  136. mp_int **factors, size_t nfactors,
  137. mp_int *primitive_root);
  138. /* If you call pockle_mark, and later pass the returned value to
  139. * pockle_release, it will free all the primes that were added to the
  140. * Pockle between those two calls. Useful in recursive algorithms, to
  141. * stop the Pockle growing unboundedly if the recursion keeps having
  142. * to backtrack. */
  143. size_t pockle_mark(Pockle *pockle);
  144. void pockle_release(Pockle *pockle, size_t mark);
  145. /* Free a Pockle. */
  146. void pockle_free(Pockle *pockle);
  147. /* Generate a certificate of primality for a prime already known to
  148. * the Pockle, in a format acceptable to Math::Prime::Util. */
  149. strbuf *pockle_mpu(Pockle *pockle, mp_int *p);
  150. /* ----------------------------------------------------------------------
  151. * Callback API that allows key generation to report progress to its
  152. * caller.
  153. */
  154. typedef struct ProgressReceiverVtable ProgressReceiverVtable;
  155. typedef struct ProgressReceiver ProgressReceiver;
  156. typedef union ProgressPhase ProgressPhase;
  157. union ProgressPhase {
  158. int n;
  159. void *p;
  160. };
  161. struct ProgressReceiver {
  162. const ProgressReceiverVtable *vt;
  163. };
  164. struct ProgressReceiverVtable {
  165. ProgressPhase (*add_linear)(ProgressReceiver *prog, double overall_cost);
  166. ProgressPhase (*add_probabilistic)(ProgressReceiver *prog,
  167. double cost_per_attempt,
  168. double attempt_probability);
  169. void (*ready)(ProgressReceiver *prog);
  170. void (*start_phase)(ProgressReceiver *prog, ProgressPhase phase);
  171. void (*report)(ProgressReceiver *prog, double progress);
  172. void (*report_attempt)(ProgressReceiver *prog);
  173. void (*report_phase_complete)(ProgressReceiver *prog);
  174. };
  175. static inline ProgressPhase progress_add_linear(ProgressReceiver *prog,
  176. double c)
  177. { return prog->vt->add_linear(prog, c); }
  178. static inline ProgressPhase progress_add_probabilistic(ProgressReceiver *prog,
  179. double c, double p)
  180. { return prog->vt->add_probabilistic(prog, c, p); }
  181. static inline void progress_ready(ProgressReceiver *prog)
  182. { prog->vt->ready(prog); }
  183. static inline void progress_start_phase(
  184. ProgressReceiver *prog, ProgressPhase phase)
  185. { prog->vt->start_phase(prog, phase); }
  186. static inline void progress_report(ProgressReceiver *prog, double progress)
  187. { prog->vt->report(prog, progress); }
  188. static inline void progress_report_attempt(ProgressReceiver *prog)
  189. { prog->vt->report_attempt(prog); }
  190. static inline void progress_report_phase_complete(ProgressReceiver *prog)
  191. { prog->vt->report_phase_complete(prog); }
  192. ProgressPhase null_progress_add_linear(
  193. ProgressReceiver *prog, double c);
  194. ProgressPhase null_progress_add_probabilistic(
  195. ProgressReceiver *prog, double c, double p);
  196. void null_progress_ready(ProgressReceiver *prog);
  197. void null_progress_start_phase(ProgressReceiver *prog, ProgressPhase phase);
  198. void null_progress_report(ProgressReceiver *prog, double progress);
  199. void null_progress_report_attempt(ProgressReceiver *prog);
  200. void null_progress_report_phase_complete(ProgressReceiver *prog);
  201. extern const ProgressReceiverVtable null_progress_vt;
  202. /* A helper function for dreaming up progress cost estimates. */
  203. double estimate_modexp_cost(unsigned bits);
  204. /* ----------------------------------------------------------------------
  205. * The top-level API for generating primes.
  206. */
  207. typedef struct PrimeGenerationPolicy PrimeGenerationPolicy;
  208. typedef struct PrimeGenerationContext PrimeGenerationContext;
  209. struct PrimeGenerationContext {
  210. const PrimeGenerationPolicy *vt;
  211. };
  212. struct PrimeGenerationPolicy {
  213. ProgressPhase (*add_progress_phase)(const PrimeGenerationPolicy *policy,
  214. ProgressReceiver *prog, unsigned bits);
  215. PrimeGenerationContext *(*new_context)(
  216. const PrimeGenerationPolicy *policy);
  217. void (*free_context)(PrimeGenerationContext *ctx);
  218. mp_int *(*generate)(
  219. PrimeGenerationContext *ctx,
  220. PrimeCandidateSource *pcs, ProgressReceiver *prog);
  221. strbuf *(*mpu_certificate)(PrimeGenerationContext *ctx, mp_int *p);
  222. const void *extra; /* additional data a particular impl might need */
  223. };
  224. static inline ProgressPhase primegen_add_progress_phase(
  225. PrimeGenerationContext *ctx, ProgressReceiver *prog, unsigned bits)
  226. { return ctx->vt->add_progress_phase(ctx->vt, prog, bits); }
  227. static inline PrimeGenerationContext *primegen_new_context(
  228. const PrimeGenerationPolicy *policy)
  229. { return policy->new_context(policy); }
  230. static inline void primegen_free_context(PrimeGenerationContext *ctx)
  231. { ctx->vt->free_context(ctx); }
  232. static inline mp_int *primegen_generate(
  233. PrimeGenerationContext *ctx,
  234. PrimeCandidateSource *pcs, ProgressReceiver *prog)
  235. { return ctx->vt->generate(ctx, pcs, prog); }
  236. static inline strbuf *primegen_mpu_certificate(
  237. PrimeGenerationContext *ctx, mp_int *p)
  238. { return ctx->vt->mpu_certificate(ctx, p); }
  239. extern const PrimeGenerationPolicy primegen_probabilistic;
  240. extern const PrimeGenerationPolicy primegen_provable_fast;
  241. extern const PrimeGenerationPolicy primegen_provable_maurer_simple;
  242. extern const PrimeGenerationPolicy primegen_provable_maurer_complex;
  243. /* ----------------------------------------------------------------------
  244. * The overall top-level API for generating entire key pairs.
  245. */
  246. int rsa_generate(RSAKey *key, int bits, bool strong,
  247. PrimeGenerationContext *pgc, ProgressReceiver *prog);
  248. int dsa_generate(struct dsa_key *key, int bits, PrimeGenerationContext *pgc,
  249. ProgressReceiver *prog);
  250. int ecdsa_generate(struct ecdsa_key *key, int bits);
  251. int eddsa_generate(struct eddsa_key *key, int bits);