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