xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hcrypto/ec.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ec.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2009 Kungliga Tekniska H�gskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "ec.h"
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc struct EC_POINT {
39ebfedea0SLionel Sambuc     int inf;
40ebfedea0SLionel Sambuc     mp_int x;
41ebfedea0SLionel Sambuc     mp_int y;
42ebfedea0SLionel Sambuc     mp_int z;
43ebfedea0SLionel Sambuc };
44ebfedea0SLionel Sambuc 
45ebfedea0SLionel Sambuc struct EC_GROUP {
46ebfedea0SLionel Sambuc     size_t size;
47ebfedea0SLionel Sambuc     mp_int prime;
48ebfedea0SLionel Sambuc     mp_int order;
49ebfedea0SLionel Sambuc     mp_int Gx;
50ebfedea0SLionel Sambuc     mp_int Gy;
51ebfedea0SLionel Sambuc };
52ebfedea0SLionel Sambuc 
53ebfedea0SLionel Sambuc struct EC_KEY {
54ebfedea0SLionel Sambuc     int type;
55ebfedea0SLionel Sambuc     EC_GROUP *group;
56ebfedea0SLionel Sambuc     EC_POINT *pubkey;
57ebfedea0SLionel Sambuc     mp_int privkey;
58ebfedea0SLionel Sambuc };
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc 
61ebfedea0SLionel Sambuc unsigned long
EC_GROUP_get_degree(EC_GROUP *)62ebfedea0SLionel Sambuc EC_GROUP_get_degree(EC_GROUP *)
63ebfedea0SLionel Sambuc {
64ebfedea0SLionel Sambuc }
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc EC_GROUP *
EC_KEY_get0_group(EC_KEY *)67ebfedea0SLionel Sambuc EC_KEY_get0_group(EC_KEY *)
68ebfedea0SLionel Sambuc {
69ebfedea0SLionel Sambuc }
70ebfedea0SLionel Sambuc 
71ebfedea0SLionel Sambuc int
EC_GROUP_get_order(EC_GROUP *,BIGNUM *,BN_CTX *)72ebfedea0SLionel Sambuc EC_GROUP_get_order(EC_GROUP *, BIGNUM *, BN_CTX *)
73ebfedea0SLionel Sambuc {
74ebfedea0SLionel Sambuc }
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc EC_KEY *
o2i_ECPublicKey(EC_KEY ** key,unsigned char **,size_t)77ebfedea0SLionel Sambuc o2i_ECPublicKey(EC_KEY **key, unsigned char **, size_t)
78ebfedea0SLionel Sambuc {
79ebfedea0SLionel Sambuc }
80ebfedea0SLionel Sambuc 
81ebfedea0SLionel Sambuc void
EC_KEY_free(EC_KEY *)82ebfedea0SLionel Sambuc EC_KEY_free(EC_KEY *)
83ebfedea0SLionel Sambuc {
84ebfedea0SLionel Sambuc 
85ebfedea0SLionel Sambuc }
86ebfedea0SLionel Sambuc 
87ebfedea0SLionel Sambuc EC_GROUP *
EC_GROUP_new_by_curve_name(int nid)88ebfedea0SLionel Sambuc EC_GROUP_new_by_curve_name(int nid)
89ebfedea0SLionel Sambuc {
90ebfedea0SLionel Sambuc }
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc EC_KEY *
EC_KEY_new_by_curve_name(EC_GROUP_ID nid)93ebfedea0SLionel Sambuc EC_KEY_new_by_curve_name(EC_GROUP_ID nid)
94ebfedea0SLionel Sambuc {
95ebfedea0SLionel Sambuc     EC_KEY *key;
96ebfedea0SLionel Sambuc 
97ebfedea0SLionel Sambuc     key = calloc(1, sizeof(*key));
98ebfedea0SLionel Sambuc     return key;
99ebfedea0SLionel Sambuc }
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc void
EC_POINT_free(EC_POINT * p)102ebfedea0SLionel Sambuc EC_POINT_free(EC_POINT *p)
103ebfedea0SLionel Sambuc {
104ebfedea0SLionel Sambuc     mp_clear_multi(&p->x, p->y, p->z, NULL);
105ebfedea0SLionel Sambuc     free(p);
106ebfedea0SLionel Sambuc }
107ebfedea0SLionel Sambuc 
108ebfedea0SLionel Sambuc static int
ec_point_mul(EC_POINT * res,const EC_GROUP * group,const mp_int * point)109ebfedea0SLionel Sambuc ec_point_mul(EC_POINT *res, const EC_GROUP *group, const mp_int *point)
110ebfedea0SLionel Sambuc {
111ebfedea0SLionel Sambuc }
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc EC_POINT *
EC_POINT_new(void)114ebfedea0SLionel Sambuc EC_POINT_new(void)
115ebfedea0SLionel Sambuc {
116ebfedea0SLionel Sambuc     EC_POINT *p;
117ebfedea0SLionel Sambuc 
118ebfedea0SLionel Sambuc     p = calloc(1, sizeof(*p));
119ebfedea0SLionel Sambuc 
120ebfedea0SLionel Sambuc     if (mp_init_multi(&p->x, &p->y, &p->z, NULL) != 0) {
121ebfedea0SLionel Sambuc 	EC_POINT_free(p);
122ebfedea0SLionel Sambuc 	return NULL;
123ebfedea0SLionel Sambuc     }
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc     return p;
126ebfedea0SLionel Sambuc }
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc int
EC_KEY_generate_key(EC_KEY * key)129ebfedea0SLionel Sambuc EC_KEY_generate_key(EC_KEY *key)
130ebfedea0SLionel Sambuc {
131ebfedea0SLionel Sambuc     int ret = 0;
132ebfedea0SLionel Sambuc 
133ebfedea0SLionel Sambuc     if (key->group == NULL)
134ebfedea0SLionel Sambuc 	return 0;
135ebfedea0SLionel Sambuc 
136ebfedea0SLionel Sambuc     do {
137ebfedea0SLionel Sambuc 	random(key->privkey, key->group->size);
138ebfedea0SLionel Sambuc     } while(mp_cmp(key->privkey, key->group->order) >= 0);
139ebfedea0SLionel Sambuc 
140ebfedea0SLionel Sambuc     if (key->pubkey == NULL)
141ebfedea0SLionel Sambuc 	key->pubkey = EC_POINT_new();
142ebfedea0SLionel Sambuc 
143ebfedea0SLionel Sambuc     if (ec_point_mul(&key->pubkey, key->group, key->privkey) != 1)
144ebfedea0SLionel Sambuc 	goto error;
145ebfedea0SLionel Sambuc 
146ebfedea0SLionel Sambuc     ret = 1;
147ebfedea0SLionel Sambuc  error:
148ebfedea0SLionel Sambuc     ECPOINT_free(&base);
149ebfedea0SLionel Sambuc 
150ebfedea0SLionel Sambuc     return ret;
151ebfedea0SLionel Sambuc }
152ebfedea0SLionel Sambuc 
153ebfedea0SLionel Sambuc void
EC_KEY_set_group(EC_KEY *,EC_GROUP *)154ebfedea0SLionel Sambuc EC_KEY_set_group(EC_KEY *, EC_GROUP *)
155ebfedea0SLionel Sambuc {
156ebfedea0SLionel Sambuc 
157ebfedea0SLionel Sambuc }
158ebfedea0SLionel Sambuc 
159ebfedea0SLionel Sambuc void
EC_GROUP_free(EC_GROUP *)160ebfedea0SLionel Sambuc EC_GROUP_free(EC_GROUP *)
161ebfedea0SLionel Sambuc {
162ebfedea0SLionel Sambuc }
163ebfedea0SLionel Sambuc 
164ebfedea0SLionel Sambuc int
EC_KEY_check_key(const EC_KEY *)165ebfedea0SLionel Sambuc EC_KEY_check_key(const EC_KEY *)
166ebfedea0SLionel Sambuc {
167ebfedea0SLionel Sambuc }
168ebfedea0SLionel Sambuc 
169ebfedea0SLionel Sambuc const BIGNUM *
EC_KEY_get0_private_key(const EC_KEY * key)170ebfedea0SLionel Sambuc EC_KEY_get0_private_key(const EC_KEY *key)
171ebfedea0SLionel Sambuc {
172ebfedea0SLionel Sambuc }
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc int
EC_KEY_set_private_key(EC_KEY * key,const BIGNUM * bn)175ebfedea0SLionel Sambuc EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *bn)
176ebfedea0SLionel Sambuc {
177ebfedea0SLionel Sambuc }
178