chiark / gitweb /
site.c: Abstract out generation of the Diffie--Hellman private secret.
[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 "util.h"
36
37 struct dh {
38     closure_t cl;
39     struct dh_if ops;
40     struct cloc loc;
41     MP_INT p,g; /* prime modulus and generator */
42 };
43
44 static string_t dh_makepublic(void *sst, uint8_t *secret, int32_t secretlen)
45 {
46     struct dh *st=sst;
47     string_t r;
48     MP_INT a, b; /* a is secret key, b is public key */
49
50     mpz_init(&a);
51     mpz_init(&b);
52
53     read_mpbin(&a, secret, secretlen);
54
55     mpz_powm_sec(&b, &st->g, &a, &st->p);
56
57     r=write_mpstring(&b);
58
59     mpz_clear(&a);
60     mpz_clear(&b);
61     return r;
62 }
63
64 static dh_makeshared_fn dh_makeshared;
65 static bool_t dh_makeshared(void *sst, uint8_t *secret, int32_t secretlen,
66                             cstring_t rempublic, uint8_t *sharedsecret,
67                             int32_t buflen)
68 {
69     struct dh *st=sst;
70     MP_INT a, b, c;
71
72     mpz_init(&a);
73     mpz_init(&b);
74     mpz_init(&c);
75
76     read_mpbin(&a, secret, secretlen);
77     mpz_set_str(&b, rempublic, 16);
78
79     mpz_powm_sec(&c, &b, &a, &st->p);
80
81     write_mpbin(&c,sharedsecret,buflen);
82
83     mpz_clear(&a);
84     mpz_clear(&b);
85     mpz_clear(&c);
86
87     return True;
88 }
89
90 static list_t *dh_apply(closure_t *self, struct cloc loc, dict_t *context,
91                         list_t *args)
92 {
93     struct dh *st;
94     string_t p,g;
95     dict_t *dict = 0;
96     item_t *i;
97     bool_t check = True;
98
99     NEW(st);
100     st->cl.description="dh";
101     st->cl.type=CL_DH;
102     st->cl.apply=NULL;
103     st->cl.interface=&st->ops;
104     st->ops.st=st;
105     st->ops.makepublic=dh_makepublic;
106     st->ops.makeshared=dh_makeshared;
107     st->loc=loc;
108
109     /* We either have two string arguments and maybe a boolean, or a
110      * dictionary
111      */
112     i=list_elem(args,0);
113     if (i && i->type==t_dict) {
114         dict=i->data.dict;
115         p=dict_read_string(dict,"p",True,"diffie-hellman",loc);
116         g=dict_read_string(dict,"g",True,"diffie-hellman",loc);
117         check=dict_read_bool(dict,"check",False,"diffie-hellman",loc,True);
118     } else {
119         if (!i)
120             cfgfatal(loc,"diffie-hellman","you must provide a prime modulus\n");
121         else if (i->type!=t_string)
122             cfgfatal(i->loc,"diffie-hellman",
123                      "first argument must be a string or a dictionary\n");
124         p=i->data.string;
125         i=list_elem(args,1);
126         if (!i)
127             cfgfatal(loc,"diffie-hellman","you must provide a generator\n");
128         else if (i->type!=t_string)
129             cfgfatal(i->loc,"diffie-hellman","second argument must be a "
130                      "string\n");
131         g=i->data.string;
132         i=list_elem(args,2);
133         if (i) {
134             if (i->type!=t_bool)
135                 cfgfatal(i->loc,"diffie-hellman",
136                          "third argument must be boolean or omitted\n");
137             check=i->data.bool;
138         }
139     }
140
141     if (mpz_init_set_str(&st->p,p,16)!=0)
142         cfgfatal(loc,"diffie-hellman","\"%s\" is not a hex number "
143                  "string\n",p);
144     if (mpz_init_set_str(&st->g,g,16)!=0)
145         cfgfatal(i->loc,"diffie-hellman","\"%s\" is not a hex number "
146                  "string\n",g);
147
148     if (!check) {
149         Message(M_INFO,"diffie-hellman (%s:%d): skipping modulus "
150                 "primality check\n",loc.file,loc.line);
151     } else {
152         /* Test that the modulus is really prime */
153         if (mpz_probab_prime_p(&st->p,5)==0) {
154             cfgfatal(loc,"diffie-hellman","modulus must be a prime\n");
155         }
156     }
157
158     size_t sz=mpz_sizeinbase(&st->p,2)/8;
159     if (sz>INT_MAX) {
160         cfgfatal(loc,"diffie-hellman","modulus far too large\n");
161     }
162     if (mpz_cmp(&st->g,&st->p) >= 0) {
163         cfgfatal(loc,"diffie-hellman","generator must be less than modulus\n");
164     }
165
166     st->ops.secret_len=sz;
167
168     st->ops.shared_len=(mpz_sizeinbase(&st->p,2)+7)/8;
169     /* According to the docs, mpz_sizeinbase(,256) is allowed to return
170      * an answer which is 1 too large.  But mpz_sizeinbase(,2) isn't. */
171
172     return new_closure(&st->cl);
173 }
174
175 void dh_module(dict_t *dict)
176 {
177     add_closure(dict,"diffie-hellman",dh_apply);
178 }