sshecdsag.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * EC key generation.
  3. */
  4. #include "ssh.h"
  5. /* Forward reference from sshecc.c */
  6. struct ec_point *ecp_mul(const struct ec_point *a, const Bignum b);
  7. int ec_generate(struct ec_key *key, int bits, progfn_t pfn,
  8. void *pfnparam)
  9. {
  10. struct ec_point *publicKey;
  11. if (!ec_nist_alg_and_curve_by_bits(bits, &key->publicKey.curve,
  12. &key->signalg))
  13. return 0;
  14. key->privateKey = bignum_random_in_range(One, key->publicKey.curve->w.n);
  15. if (!key->privateKey) return 0;
  16. publicKey = ec_public(key->privateKey, key->publicKey.curve);
  17. if (!publicKey) {
  18. freebn(key->privateKey);
  19. key->privateKey = NULL;
  20. return 0;
  21. }
  22. key->publicKey.x = publicKey->x;
  23. key->publicKey.y = publicKey->y;
  24. key->publicKey.z = NULL;
  25. sfree(publicKey);
  26. return 1;
  27. }
  28. int ec_edgenerate(struct ec_key *key, int bits, progfn_t pfn,
  29. void *pfnparam)
  30. {
  31. struct ec_point *publicKey;
  32. if (!ec_ed_alg_and_curve_by_bits(bits, &key->publicKey.curve,
  33. &key->signalg))
  34. return 0;
  35. {
  36. /* EdDSA secret keys are just 32 bytes of hash preimage; the
  37. * 64-byte SHA-512 hash of that key will be used when signing,
  38. * but the form of the key stored on disk is the preimage
  39. * only. */
  40. Bignum privMax = bn_power_2(bits);
  41. if (!privMax) return 0;
  42. key->privateKey = bignum_random_in_range(Zero, privMax);
  43. freebn(privMax);
  44. if (!key->privateKey) return 0;
  45. }
  46. publicKey = ec_public(key->privateKey, key->publicKey.curve);
  47. if (!publicKey) {
  48. freebn(key->privateKey);
  49. key->privateKey = NULL;
  50. return 0;
  51. }
  52. key->publicKey.x = publicKey->x;
  53. key->publicKey.y = publicKey->y;
  54. key->publicKey.z = NULL;
  55. sfree(publicKey);
  56. return 1;
  57. }