1 /***************************************************************************
3 * Part II Project, "A secure, private IP network"
4 * Stephen Early <sde1000@cam.ac.uk>
9 * Description: Diffie-Hellman implementation
11 * Copyright: (C) Stephen Early 1995
15 * $Date: 1996/05/16 18:38:54 $
19 ***************************************************************************/
23 * Revision 1.3 1996/05/16 18:38:54 sde1000
24 * Removed unused hexdigits variable.
26 * Revision 1.2 1996/04/14 16:33:52 sde1000
27 * Moved mpbin/mpstring functions into util.c
29 * Revision 1.1 1996/04/14 16:21:47 sde1000
44 MP_INT p,g; /* prime modulus and generator */
47 static string_t dh_makepublic(void *sst, uint8_t *secret, uint32_t secretlen)
51 MP_INT a, b; /* a is secret key, b is public key */
56 read_mpbin(&a, secret, secretlen);
58 mpz_powm(&b, &st->g, &a, &st->p);
67 static void dh_makeshared(void *sst, uint8_t *secret, uint32_t secretlen,
68 string_t rempublic, uint8_t *sharedsecret,
78 read_mpbin(&a, secret, secretlen);
79 mpz_set_str(&b, rempublic, 16);
81 mpz_powm(&c, &b, &a, &st->p);
83 write_mpbin(&c,sharedsecret,buflen);
90 static list_t *dh_apply(closure_t *self, struct cloc loc, dict_t *context,
97 st=safe_malloc(sizeof(*st),"dh_apply");
98 st->cl.description="dh";
101 st->cl.interface=&st->ops;
103 st->ops.makepublic=dh_makepublic;
104 st->ops.makeshared=dh_makeshared;
106 /* We have two string arguments: the first is the modulus, and the
107 second is the generator. Both are in hex. */
110 if (i->type!=t_string) {
111 cfgfatal(i->loc,"diffie-hellman","first argument must be a "
115 if (mpz_init_set_str(&st->p,p,16)!=0) {
116 cfgfatal(i->loc,"diffie-hellman","\"%s\" is not a hex number "
120 cfgfatal(loc,"diffie-hellman","you must provide a prime modulus\n");
125 if (i->type!=t_string) {
126 cfgfatal(i->loc,"diffie-hellman","second argument must be a "
130 if (mpz_init_set_str(&st->g,g,16)!=0) {
131 cfgfatal(i->loc,"diffie-hellman","\"%s\" is not a hex number "
135 cfgfatal(loc,"diffie-hellman","you must provide a generator\n");
139 if (i && i->type==t_bool && i->data.bool==False) {
140 Message(M_INFO,"diffie-hellman (%s:%d): skipping modulus "
141 "primality check\n",loc.file,loc.line);
143 /* Test that the modulus is really prime */
144 if (mpz_probab_prime_p(&st->p,5)==0) {
145 cfgfatal(loc,"diffie-hellman","modulus must be a prime\n");
148 st->ops.len=mpz_sizeinbase(&st->p,2)/8;
150 return new_closure(&st->cl);
153 init_module dh_module;
154 void dh_module(dict_t *dict)
156 add_closure(dict,"diffie-hellman",dh_apply);