]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sshecdsag.c
first pass
[PuTTY.git] / sshecdsag.c
1 /*
2  * EC key generation.
3  */
4
5 #include "ssh.h"
6
7 /* Forward reference from sshecc.c */
8 struct ec_point *ecp_mul(const struct ec_point *a, const Bignum b);
9
10 int ec_generate(struct ec_key *key, int bits, progfn_t pfn,
11                 void *pfnparam)
12 {
13     struct ec_point *publicKey;
14
15     if (!ec_nist_alg_and_curve_by_bits(bits, &key->publicKey.curve,
16                                        &key->signalg))
17         return 0;
18
19     key->privateKey = bignum_random_in_range(One, key->publicKey.curve->w.n);
20     if (!key->privateKey) return 0;
21
22     publicKey = ec_public(key->privateKey, key->publicKey.curve);
23     if (!publicKey) {
24         freebn(key->privateKey);
25         key->privateKey = NULL;
26         return 0;
27     }
28
29     key->publicKey.x = publicKey->x;
30     key->publicKey.y = publicKey->y;
31     key->publicKey.z = NULL;
32     sfree(publicKey);
33
34     return 1;
35 }
36
37 int ec_edgenerate(struct ec_key *key, int bits, progfn_t pfn,
38                   void *pfnparam)
39 {
40     struct ec_point *publicKey;
41
42     if (!ec_ed_alg_and_curve_by_bits(bits, &key->publicKey.curve,
43                                      &key->signalg))
44         return 0;
45
46     {
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;
56     }
57
58     publicKey = ec_public(key->privateKey, key->publicKey.curve);
59     if (!publicKey) {
60         freebn(key->privateKey);
61         key->privateKey = NULL;
62         return 0;
63     }
64
65     key->publicKey.x = publicKey->x;
66     key->publicKey.y = publicKey->y;
67     key->publicKey.z = NULL;
68     sfree(publicKey);
69
70     return 1;
71 }