libsec: add diffie-hellman functions
This commit is contained in:
parent
4cf00ca6cb
commit
6c68876db6
3 changed files with 60 additions and 0 deletions
|
@ -439,3 +439,22 @@ void base58enc(uchar *, char *, int);
|
||||||
int base58dec(char *, uchar *, int);
|
int base58dec(char *, uchar *, int);
|
||||||
|
|
||||||
DigestState* ripemd160(uchar *, ulong, uchar *, DigestState *);
|
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);
|
||||||
|
|
40
sys/src/libsec/port/dh.c
Normal file
40
sys/src/libsec/port/dh.c
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ CFILES = des.c desmodes.c desECB.c desCBC.c des3ECB.c des3CBC.c\
|
||||||
aes_xts.c \
|
aes_xts.c \
|
||||||
ecc.c\
|
ecc.c\
|
||||||
ripemd.c\
|
ripemd.c\
|
||||||
|
dh.c\
|
||||||
|
|
||||||
ALLOFILES=${CFILES:%.c=%.$O}
|
ALLOFILES=${CFILES:%.c=%.$O}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue