diff options
| author | cinap_lenrek <cinap_lenrek@felloff.net> | 2014-09-14 02:29:18 +0200 |
|---|---|---|
| committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2014-09-14 02:29:18 +0200 |
| commit | 6c68876db6d25b8c646295fecc75a6363d0bdc75 (patch) | |
| tree | 26017f9d8aa6fab798a3cc762b8b274d86bd033d | |
| parent | 4cf00ca6cb40918c8ca89aebf02e8ca41c857e94 (diff) | |
| download | plan9front-6c68876db6d25b8c646295fecc75a6363d0bdc75.tar.xz | |
libsec: add diffie-hellman functions
| -rw-r--r-- | sys/include/libsec.h | 19 | ||||
| -rw-r--r-- | sys/src/libsec/port/dh.c | 40 | ||||
| -rw-r--r-- | sys/src/libsec/port/mkfile | 1 |
3 files changed, 60 insertions, 0 deletions
diff --git a/sys/include/libsec.h b/sys/include/libsec.h index a9c24b91c..4ab074fdb 100644 --- a/sys/include/libsec.h +++ b/sys/include/libsec.h @@ -439,3 +439,22 @@ void base58enc(uchar *, char *, int); int base58dec(char *, uchar *, int); DigestState* ripemd160(uchar *, ulong, uchar *, DigestState *); + +/* + * Diffie-Hellman key exchange + */ + +typedef struct DHstate DHstate; +struct DHstate +{ + mpint *g; /* base g */ + mpint *p; /* large prime */ + mpint *x; /* random secret */ + mpint *y; /* public key y = g ^ x % p */ +}; + +/* generate new public key: y = g ^ x % p */ +mpint* dh_new(DHstate *dh, mpint *p, mpint *g); + +/* calculate shared key: k = pub ^ x % p */ +mpint* dh_finish(DHstate *dh, mpint *pub); diff --git a/sys/src/libsec/port/dh.c b/sys/src/libsec/port/dh.c new file mode 100644 index 000000000..70f6a864c --- /dev/null +++ b/sys/src/libsec/port/dh.c @@ -0,0 +1,40 @@ +#include "os.h" +#include <mp.h> +#include <libsec.h> + +mpint* +dh_new(DHstate *dh, mpint *p, mpint *g) +{ + memset(dh, 0, sizeof(*dh)); + dh->g = mpcopy(g); + dh->p = mpcopy(p); + if(dh->g != nil && dh->p != nil){ + dh->x = mprand(mpsignif(dh->p), genrandom, nil); + dh->y = mpnew(0); + if(dh->x != nil && dh->y != nil){ + mpexp(dh->g, dh->x, dh->p, dh->y); + return dh->y; + } + } + dh_finish(dh, nil); + return nil; +} + +mpint* +dh_finish(DHstate *dh, mpint *pub) +{ + mpint *k; + + k = nil; + if(pub != nil && dh->x != nil && dh->p != nil){ + if((k = mpnew(0)) != nil) + mpexp(pub, dh->x, dh->p, k); + } + mpfree(dh->g); + mpfree(dh->p); + mpfree(dh->x); + mpfree(dh->y); + memset(dh, 0, sizeof(*dh)); + return k; +} + diff --git a/sys/src/libsec/port/mkfile b/sys/src/libsec/port/mkfile index 59d2f17ac..c99fcc85d 100644 --- a/sys/src/libsec/port/mkfile +++ b/sys/src/libsec/port/mkfile @@ -20,6 +20,7 @@ CFILES = des.c desmodes.c desECB.c desCBC.c des3ECB.c des3CBC.c\ aes_xts.c \ ecc.c\ ripemd.c\ + dh.c\ ALLOFILES=${CFILES:%.c=%.$O} |
