Import release 0.1.15
[secnet] / dh.c
1 #include <stdio.h>
2 #include <gmp.h>
3
4 #include "secnet.h"
5 #include "util.h"
6
7 struct dh {
8 closure_t cl;
9 struct dh_if ops;
10 struct cloc loc;
11 MP_INT p,g; /* prime modulus and generator */
12 };
13
14 static string_t dh_makepublic(void *sst, uint8_t *secret, uint32_t secretlen)
15 {
16 struct dh *st=sst;
17 string_t r;
18 MP_INT a, b; /* a is secret key, b is public key */
19
20 mpz_init(&a);
21 mpz_init(&b);
22
23 read_mpbin(&a, secret, secretlen);
24
25 mpz_powm(&b, &st->g, &a, &st->p);
26
27 r=write_mpstring(&b);
28
29 mpz_clear(&a);
30 mpz_clear(&b);
31 return r;
32 }
33
34 static dh_makeshared_fn dh_makeshared;
35 static void dh_makeshared(void *sst, uint8_t *secret, uint32_t secretlen,
36 cstring_t rempublic, uint8_t *sharedsecret,
37 uint32_t buflen)
38 {
39 struct dh *st=sst;
40 MP_INT a, b, c;
41
42 mpz_init(&a);
43 mpz_init(&b);
44 mpz_init(&c);
45
46 read_mpbin(&a, secret, secretlen);
47 mpz_set_str(&b, rempublic, 16);
48
49 mpz_powm(&c, &b, &a, &st->p);
50
51 write_mpbin(&c,sharedsecret,buflen);
52
53 mpz_clear(&a);
54 mpz_clear(&b);
55 mpz_clear(&c);
56 }
57
58 static list_t *dh_apply(closure_t *self, struct cloc loc, dict_t *context,
59 list_t *args)
60 {
61 struct dh *st;
62 string_t p,g;
63 item_t *i;
64
65 st=safe_malloc(sizeof(*st),"dh_apply");
66 st->cl.description="dh";
67 st->cl.type=CL_DH;
68 st->cl.apply=NULL;
69 st->cl.interface=&st->ops;
70 st->ops.st=st;
71 st->ops.makepublic=dh_makepublic;
72 st->ops.makeshared=dh_makeshared;
73 st->loc=loc;
74 /* We have two string arguments: the first is the modulus, and the
75 second is the generator. Both are in hex. */
76 i=list_elem(args,0);
77 if (i) {
78 if (i->type!=t_string) {
79 cfgfatal(i->loc,"diffie-hellman","first argument must be a "
80 "string\n");
81 }
82 p=i->data.string;
83 if (mpz_init_set_str(&st->p,p,16)!=0) {
84 cfgfatal(i->loc,"diffie-hellman","\"%s\" is not a hex number "
85 "string\n",p);
86 }
87 } else {
88 cfgfatal(loc,"diffie-hellman","you must provide a prime modulus\n");
89 }
90
91 i=list_elem(args,1);
92 if (i) {
93 if (i->type!=t_string) {
94 cfgfatal(i->loc,"diffie-hellman","second argument must be a "
95 "string\n");
96 }
97 g=i->data.string;
98 if (mpz_init_set_str(&st->g,g,16)!=0) {
99 cfgfatal(i->loc,"diffie-hellman","\"%s\" is not a hex number "
100 "string\n",g);
101 }
102 } else {
103 cfgfatal(loc,"diffie-hellman","you must provide a generator\n");
104 }
105
106 i=list_elem(args,2);
107 if (i && i->type==t_bool && i->data.bool==False) {
108 Message(M_INFO,"diffie-hellman (%s:%d): skipping modulus "
109 "primality check\n",loc.file,loc.line);
110 } else {
111 /* Test that the modulus is really prime */
112 if (mpz_probab_prime_p(&st->p,5)==0) {
113 cfgfatal(loc,"diffie-hellman","modulus must be a prime\n");
114 }
115 }
116 st->ops.len=mpz_sizeinbase(&st->p,2)/8;
117
118 return new_closure(&st->cl);
119 }
120
121 init_module dh_module;
122 void dh_module(dict_t *dict)
123 {
124 add_closure(dict,"diffie-hellman",dh_apply);
125 }