ecdsa.c 916 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * EC key generation.
  3. */
  4. #include "ssh.h"
  5. #include "sshkeygen.h"
  6. #include "mpint.h"
  7. int ecdsa_generate(struct ecdsa_key *ek, int bits)
  8. {
  9. if (!ec_nist_alg_and_curve_by_bits(bits, &ek->curve, &ek->sshk.vt))
  10. return 0;
  11. mp_int *one = mp_from_integer(1);
  12. ek->privateKey = mp_random_in_range(one, ek->curve->w.G_order);
  13. mp_free(one);
  14. ek->publicKey = ecdsa_public(ek->privateKey, ek->sshk.vt);
  15. return 1;
  16. }
  17. int eddsa_generate(struct eddsa_key *ek, int bits)
  18. {
  19. if (!ec_ed_alg_and_curve_by_bits(bits, &ek->curve, &ek->sshk.vt))
  20. return 0;
  21. /* EdDSA secret keys are just 32 bytes of hash preimage; the
  22. * 64-byte SHA-512 hash of that key will be used when signing,
  23. * but the form of the key stored on disk is the preimage
  24. * only. */
  25. ek->privateKey = mp_random_bits(bits);
  26. ek->publicKey = eddsa_public(ek->privateKey, ek->sshk.vt);
  27. return 1;
  28. }