| 1 | #include <stdio.h> |
| 2 | #include <gmp.h> |
| 3 | #include <limits.h> |
| 4 | |
| 5 | #include "secnet.h" |
| 6 | #include "util.h" |
| 7 | |
| 8 | struct 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 | |
| 15 | static string_t dh_makepublic(void *sst, uint8_t *secret, int32_t secretlen) |
| 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 | |
| 35 | static dh_makeshared_fn dh_makeshared; |
| 36 | static void dh_makeshared(void *sst, uint8_t *secret, int32_t secretlen, |
| 37 | cstring_t rempublic, uint8_t *sharedsecret, |
| 38 | int32_t buflen) |
| 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 | |
| 59 | static 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 | |
| 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 | } |
| 116 | } |
| 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; |
| 127 | |
| 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 | |
| 132 | return new_closure(&st->cl); |
| 133 | } |
| 134 | |
| 135 | void dh_module(dict_t *dict) |
| 136 | { |
| 137 | add_closure(dict,"diffie-hellman",dh_apply); |
| 138 | } |