CPatch: New memory management
[TortoiseGit.git] / src / TortoisePlink / sshecdsag.c
blob4d42180f876b5f4c1f5e0d800a3a678c8fe2779c
1 /*
2 * EC key generation.
3 */
5 #include "ssh.h"
7 /* Forward reference from sshecc.c */
8 struct ec_point *ecp_mul(const struct ec_point *a, const Bignum b);
10 int ec_generate(struct ec_key *key, int bits, progfn_t pfn,
11 void *pfnparam)
13 struct ec_point *publicKey;
15 if (!ec_nist_alg_and_curve_by_bits(bits, &key->publicKey.curve,
16 &key->signalg))
17 return 0;
19 key->privateKey = bignum_random_in_range(One, key->publicKey.curve->w.n);
20 if (!key->privateKey) return 0;
22 publicKey = ec_public(key->privateKey, key->publicKey.curve);
23 if (!publicKey) {
24 freebn(key->privateKey);
25 key->privateKey = NULL;
26 return 0;
29 key->publicKey.x = publicKey->x;
30 key->publicKey.y = publicKey->y;
31 key->publicKey.z = NULL;
32 sfree(publicKey);
34 return 1;
37 int ec_edgenerate(struct ec_key *key, int bits, progfn_t pfn,
38 void *pfnparam)
40 struct ec_point *publicKey;
42 if (!ec_ed_alg_and_curve_by_bits(bits, &key->publicKey.curve,
43 &key->signalg))
44 return 0;
47 /* EdDSA secret keys are just 32 bytes of hash preimage; the
48 * 64-byte SHA-512 hash of that key will be used when signing,
49 * but the form of the key stored on disk is the preimage
50 * only. */
51 Bignum privMax = bn_power_2(bits);
52 if (!privMax) return 0;
53 key->privateKey = bignum_random_in_range(Zero, privMax);
54 freebn(privMax);
55 if (!key->privateKey) return 0;
58 publicKey = ec_public(key->privateKey, key->publicKey.curve);
59 if (!publicKey) {
60 freebn(key->privateKey);
61 key->privateKey = NULL;
62 return 0;
65 key->publicKey.x = publicKey->x;
66 key->publicKey.y = publicKey->y;
67 key->publicKey.z = NULL;
68 sfree(publicKey);
70 return 1;