]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sshecdsag.c
Support public keys using the "ssh-ed25519" method.
[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 (bits == 256) {
16         key->publicKey.curve = ec_p256();
17     } else if (bits == 384) {
18         key->publicKey.curve = ec_p384();
19     } else if (bits == 521) {
20         key->publicKey.curve = ec_p521();
21     } else {
22         return 0;
23     }
24
25     key->privateKey = bignum_random_in_range(One, key->publicKey.curve->w.n);
26     if (!key->privateKey) return 0;
27
28     publicKey = ec_public(key->privateKey, key->publicKey.curve);
29     if (!publicKey) {
30         freebn(key->privateKey);
31         key->privateKey = NULL;
32         return 0;
33     }
34
35     key->publicKey.x = publicKey->x;
36     key->publicKey.y = publicKey->y;
37     key->publicKey.z = NULL;
38     sfree(publicKey);
39
40     return 1;
41 }
42
43 int ec_edgenerate(struct ec_key *key, int bits, progfn_t pfn,
44                   void *pfnparam)
45 {
46     struct ec_point *publicKey;
47
48     if (bits == 256) {
49         key->publicKey.curve = ec_ed25519();
50     } else {
51         return 0;
52     }
53
54     {
55         /* EdDSA secret keys are just 32 bytes of hash preimage; the
56          * 64-byte SHA-512 hash of that key will be used when signing,
57          * but the form of the key stored on disk is the preimage
58          * only. */
59         Bignum privMax = bn_power_2(bits);
60         if (!privMax) return 0;
61         key->privateKey = bignum_random_in_range(Zero, privMax);
62         freebn(privMax);
63         if (!key->privateKey) return 0;
64     }
65
66     publicKey = ec_public(key->privateKey, key->publicKey.curve);
67     if (!publicKey) {
68         freebn(key->privateKey);
69         key->privateKey = NULL;
70         return 0;
71     }
72
73     key->publicKey.x = publicKey->x;
74     key->publicKey.y = publicKey->y;
75     key->publicKey.z = NULL;
76     sfree(publicKey);
77
78     return 1;
79 }