5 * This file is Free Software. It was originally written for secnet.
7 * Copyright 1995-2003 Stephen Early
8 * Copyright 2002-2014 Ian Jackson
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
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
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.
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.
42 MP_INT p,g; /* prime modulus and generator */
45 static string_t dh_makepublic(void *sst, uint8_t *secret, int32_t secretlen)
49 MP_INT a, b; /* a is secret key, b is public key */
54 read_mpbin(&a, secret, secretlen);
56 mpz_powm_sec(&b, &st->g, &a, &st->p);
65 static dh_makeshared_fn dh_makeshared;
66 static bool_t dh_makeshared(void *sst, uint8_t *secret, int32_t secretlen,
67 cstring_t rempublic, uint8_t *sharedsecret,
77 read_mpbin(&a, secret, secretlen);
78 mpz_set_str(&b, rempublic, 16);
80 mpz_powm_sec(&c, &b, &a, &st->p);
82 write_mpbin(&c,sharedsecret,buflen);
91 static list_t *dh_apply(closure_t *self, struct cloc loc, dict_t *context,
101 st->cl.description="dh";
104 st->cl.interface=&st->ops;
106 st->ops.makepublic=dh_makepublic;
107 st->ops.makeshared=dh_makeshared;
110 /* We either have two string arguments and maybe a boolean, or a
114 if (i && i->type==t_dict) {
116 p=dict_read_string(dict,"p",True,"diffie-hellman",loc);
117 g=dict_read_string(dict,"g",True,"diffie-hellman",loc);
118 check=dict_read_bool(dict,"check",False,"diffie-hellman",loc,True);
121 cfgfatal(loc,"diffie-hellman","you must provide a prime modulus\n");
122 else if (i->type!=t_string)
123 cfgfatal(i->loc,"diffie-hellman",
124 "first argument must be a string or a dictionary\n");
128 cfgfatal(loc,"diffie-hellman","you must provide a generator\n");
129 else if (i->type!=t_string)
130 cfgfatal(i->loc,"diffie-hellman","second argument must be a "
136 cfgfatal(i->loc,"diffie-hellman",
137 "third argument must be boolean or omitted\n");
142 if (mpz_init_set_str(&st->p,p,16)!=0)
143 cfgfatal(loc,"diffie-hellman","\"%s\" is not a hex number "
145 if (mpz_init_set_str(&st->g,g,16)!=0)
146 cfgfatal(i->loc,"diffie-hellman","\"%s\" is not a hex number "
150 Message(M_INFO,"diffie-hellman (%s:%d): skipping modulus "
151 "primality check\n",loc.file,loc.line);
153 /* Test that the modulus is really prime */
154 if (mpz_probab_prime_p(&st->p,5)==0) {
155 cfgfatal(loc,"diffie-hellman","modulus must be a prime\n");
159 size_t sz=mpz_sizeinbase(&st->p,2)/8;
161 cfgfatal(loc,"diffie-hellman","modulus far too large\n");
163 if (mpz_cmp(&st->g,&st->p) >= 0) {
164 cfgfatal(loc,"diffie-hellman","generator must be less than modulus\n");
167 st->ops.secret_len=sz;
169 st->ops.shared_len=(mpz_sizeinbase(&st->p,2)+7)/8;
170 /* According to the docs, mpz_sizeinbase(,256) is allowed to return
171 * an answer which is 1 too large. But mpz_sizeinbase(,2) isn't. */
174 st->ops.capab_bit = CAPAB_BIT_TRADZP;
176 st->ops.capab_bit = dict_read_number(dict, "capab-num", False,
177 "dh", loc, CAPAB_BIT_TRADZP);
178 if (st->ops.capab_bit > CAPAB_BIT_MAX)
179 cfgfatal(loc,"dh","capab-num out of range 0..%d\n",
182 return new_closure(&st->cl);
185 void dh_module(dict_t *dict)
187 add_closure(dict,"diffie-hellman",dh_apply);