chiark / gitweb /
ec-field-test.c: Make the field-element type use internal format.
[secnet] / dh.c
1 /*
2  * dh.c
3  */
4 /*
5  * This file is Free Software.  It was originally written for secnet.
6  *
7  * Copyright 1995-2003 Stephen Early
8  * Copyright 2002-2014 Ian Jackson
9  *
10  * You may redistribute secnet as a whole and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3, or (at your option) any
13  * later version.
14  *
15  * You may redistribute this file and/or modify it under the terms of
16  * the GNU General Public License as published by the Free Software
17  * Foundation; either version 2, or (at your option) any later
18  * version.
19  *
20  * This software is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this software; if not, see
27  * https://www.gnu.org/licenses/gpl.html.
28  */
29
30 #include <stdio.h>
31 #include <gmp.h>
32 #include <limits.h>
33
34 #include "secnet.h"
35 #include "magic.h"
36 #include "util.h"
37
38 struct dh {
39     closure_t cl;
40     struct dh_if ops;
41     struct cloc loc;
42     MP_INT p,g; /* prime modulus and generator */
43 };
44
45 static int32_t dh_makepublic(void *sst, void *public, int32_t publiclen,
46                              uint8_t *secret, int32_t secretlen)
47 {
48     struct dh *st=sst;
49     MP_INT a, b; /* a is secret key, b is public key */
50
51     mpz_init(&a);
52     mpz_init(&b);
53
54     read_mpbin(&a, secret, secretlen);
55
56     mpz_powm_sec(&b, &st->g, &a, &st->p);
57
58     assert(mpz_sizeinbase(&b, 16) + 2 <= (size_t)publiclen);
59     mpz_get_str(public, 16, &b);
60
61     mpz_clear(&a);
62     mpz_clear(&b);
63
64     return strlen(public);
65 }
66
67 static dh_makeshared_fn dh_makeshared;
68 static bool_t dh_makeshared(void *sst, uint8_t *secret, int32_t secretlen,
69                             const void *public, int32_t publiclen,
70                             uint8_t *sharedsecret, int32_t buflen)
71 {
72     struct dh *st=sst;
73     MP_INT a, b, c;
74
75     mpz_init(&a);
76     mpz_init(&b);
77     mpz_init(&c);
78
79     read_mpbin(&a, secret, secretlen);
80     mpz_set_str(&b, public, 16);
81
82     mpz_powm_sec(&c, &b, &a, &st->p);
83
84     write_mpbin(&c,sharedsecret,buflen);
85
86     mpz_clear(&a);
87     mpz_clear(&b);
88     mpz_clear(&c);
89
90     return True;
91 }
92
93 static list_t *dh_apply(closure_t *self, struct cloc loc, dict_t *context,
94                         list_t *args)
95 {
96     struct dh *st;
97     string_t p,g;
98     dict_t *dict = 0;
99     item_t *i;
100     bool_t check = True;
101
102     NEW(st);
103     st->cl.description="dh";
104     st->cl.type=CL_DH;
105     st->cl.apply=NULL;
106     st->cl.interface=&st->ops;
107     st->ops.st=st;
108     st->ops.makepublic=dh_makepublic;
109     st->ops.makeshared=dh_makeshared;
110     st->loc=loc;
111
112     /* We either have two string arguments and maybe a boolean, or a
113      * dictionary
114      */
115     i=list_elem(args,0);
116     if (i && i->type==t_dict) {
117         dict=i->data.dict;
118         p=dict_read_string(dict,"p",True,"diffie-hellman",loc);
119         g=dict_read_string(dict,"g",True,"diffie-hellman",loc);
120         check=dict_read_bool(dict,"check",False,"diffie-hellman",loc,True);
121     } else {
122         if (!i)
123             cfgfatal(loc,"diffie-hellman","you must provide a prime modulus\n");
124         else if (i->type!=t_string)
125             cfgfatal(i->loc,"diffie-hellman",
126                      "first argument must be a string or a dictionary\n");
127         p=i->data.string;
128         i=list_elem(args,1);
129         if (!i)
130             cfgfatal(loc,"diffie-hellman","you must provide a generator\n");
131         else if (i->type!=t_string)
132             cfgfatal(i->loc,"diffie-hellman","second argument must be a "
133                      "string\n");
134         g=i->data.string;
135         i=list_elem(args,2);
136         if (i) {
137             if (i->type!=t_bool)
138                 cfgfatal(i->loc,"diffie-hellman",
139                          "third argument must be boolean or omitted\n");
140             check=i->data.bool;
141         }
142     }
143
144     if (mpz_init_set_str(&st->p,p,16)!=0)
145         cfgfatal(loc,"diffie-hellman","\"%s\" is not a hex number "
146                  "string\n",p);
147     if (mpz_init_set_str(&st->g,g,16)!=0)
148         cfgfatal(i->loc,"diffie-hellman","\"%s\" is not a hex number "
149                  "string\n",g);
150
151     if (!check) {
152         Message(M_INFO,"diffie-hellman (%s:%d): skipping modulus "
153                 "primality check\n",loc.file,loc.line);
154     } else {
155         /* Test that the modulus is really prime */
156         if (mpz_probab_prime_p(&st->p,5)==0) {
157             cfgfatal(loc,"diffie-hellman","modulus must be a prime\n");
158         }
159     }
160
161     size_t sz=mpz_sizeinbase(&st->p,2)/8;
162     if (sz>INT_MAX) {
163         cfgfatal(loc,"diffie-hellman","modulus far too large\n");
164     }
165     if (mpz_cmp(&st->g,&st->p) >= 0) {
166         cfgfatal(loc,"diffie-hellman","generator must be less than modulus\n");
167     }
168
169     st->ops.secret_len=sz;
170
171     st->ops.shared_len=(mpz_sizeinbase(&st->p,2)+7)/8;
172     /* According to the docs, mpz_sizeinbase(,256) is allowed to return
173      * an answer which is 1 too large.  But mpz_sizeinbase(,2) isn't. */
174
175     st->ops.public_len=(mpz_sizeinbase(&st->p,16)+2);
176
177     if (!dict)
178         st->ops.capab_bit = CAPAB_BIT_TRADZP;
179     else
180         st->ops.capab_bit = dict_read_number(dict, "capab-num", False,
181                                              "dh", loc, CAPAB_BIT_TRADZP);
182     if (st->ops.capab_bit > CAPAB_BIT_MAX)
183         cfgfatal(loc,"dh","capab-num out of range 0..%d\n",
184                  CAPAB_BIT_MAX);
185
186     return new_closure(&st->cl);
187 }
188
189 void dh_module(dict_t *dict)
190 {
191     add_closure(dict,"diffie-hellman",dh_apply);
192 }