chiark / gitweb /
Import release 0.1.6
[secnet.git] / dh.c
1 /***************************************************************************
2  *
3  *              Part II Project, "A secure, private IP network"
4  *              Stephen Early <sde1000@cam.ac.uk>
5  *   
6  *
7  *     $RCSfile: dh.c,v $
8  *
9  *  Description: Diffie-Hellman implementation
10  *
11  *    Copyright: (C) Stephen Early 1995
12  *
13  *    $Revision: 1.3 $
14  *
15  *        $Date: 1996/05/16 18:38:54 $
16  *
17  *       $State: Exp $
18  *
19  ***************************************************************************/
20
21 /*
22  * $Log: dh.c,v $
23  * Revision 1.3  1996/05/16 18:38:54  sde1000
24  * Removed unused hexdigits variable.
25  *
26  * Revision 1.2  1996/04/14 16:33:52  sde1000
27  * Moved mpbin/mpstring functions into util.c
28  *
29  * Revision 1.1  1996/04/14 16:21:47  sde1000
30  * Initial revision
31  *
32  */
33
34 #include <stdio.h>
35 #include <gmp.h>
36
37 #include "secnet.h"
38 #include "util.h"
39
40 struct dh {
41     closure_t cl;
42     struct dh_if ops;
43     struct cloc loc;
44     MP_INT p,g; /* prime modulus and generator */
45 };
46
47 static string_t dh_makepublic(void *sst, uint8_t *secret, uint32_t secretlen)
48 {
49     struct dh *st=sst;
50     string_t r;
51     MP_INT a, b; /* a is secret key, b is public key */
52
53     mpz_init(&a);
54     mpz_init(&b);
55
56     read_mpbin(&a, secret, secretlen);
57
58     mpz_powm(&b, &st->g, &a, &st->p);
59
60     r=write_mpstring(&b);
61
62     mpz_clear(&a);
63     mpz_clear(&b);
64     return r;
65 }
66
67 static void dh_makeshared(void *sst, uint8_t *secret, uint32_t secretlen,
68                           string_t rempublic, uint8_t *sharedsecret,
69                           uint32_t buflen)
70 {
71     struct dh *st=sst;
72     MP_INT a, b, c;
73
74     mpz_init(&a);
75     mpz_init(&b);
76     mpz_init(&c);
77
78     read_mpbin(&a, secret, secretlen);
79     mpz_set_str(&b, rempublic, 16);
80
81     mpz_powm(&c, &b, &a, &st->p);
82
83     write_mpbin(&c,sharedsecret,buflen);
84
85     mpz_clear(&a);
86     mpz_clear(&b);
87     mpz_clear(&c);
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     item_t *i;
96
97     st=safe_malloc(sizeof(*st),"dh_apply");
98     st->cl.description="dh";
99     st->cl.type=CL_DH;
100     st->cl.apply=NULL;
101     st->cl.interface=&st->ops;
102     st->ops.st=st;
103     st->ops.makepublic=dh_makepublic;
104     st->ops.makeshared=dh_makeshared;
105     st->loc=loc;
106     /* We have two string arguments: the first is the modulus, and the
107        second is the generator. Both are in hex. */
108     i=list_elem(args,0);
109     if (i) {
110         if (i->type!=t_string) {
111             cfgfatal(i->loc,"diffie-hellman","first argument must be a "
112                      "string\n");
113         }
114         p=i->data.string;
115         if (mpz_init_set_str(&st->p,p,16)!=0) {
116             cfgfatal(i->loc,"diffie-hellman","\"%s\" is not a hex number "
117                      "string\n",p);
118         }
119     } else {
120         cfgfatal(loc,"diffie-hellman","you must provide a prime modulus\n");
121     }
122     
123     i=list_elem(args,1);
124     if (i) {
125         if (i->type!=t_string) {
126             cfgfatal(i->loc,"diffie-hellman","second argument must be a "
127                      "string\n");
128         }
129         g=i->data.string;
130         if (mpz_init_set_str(&st->g,g,16)!=0) {
131             cfgfatal(i->loc,"diffie-hellman","\"%s\" is not a hex number "
132                      "string\n",g);
133         }
134     } else {
135         cfgfatal(loc,"diffie-hellman","you must provide a generator\n");
136     }
137
138     i=list_elem(args,2);
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);
142     } else {
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");
146         }
147     }
148     st->ops.len=mpz_sizeinbase(&st->p,2)/8;
149
150     return new_closure(&st->cl);
151 }
152
153 init_module dh_module;
154 void dh_module(dict_t *dict)
155 {
156     add_closure(dict,"diffie-hellman",dh_apply);
157 }