diff options
Diffstat (limited to 'libkeyring')
| -rw-r--r-- | libkeyring/NOTICE | 25 | ||||
| -rw-r--r-- | libkeyring/dsaalg.c | 203 | ||||
| -rw-r--r-- | libkeyring/egalg.c | 212 | ||||
| -rw-r--r-- | libkeyring/keys.h | 118 | ||||
| -rw-r--r-- | libkeyring/mkfile | 17 | ||||
| -rw-r--r-- | libkeyring/rsaalg.c | 211 |
6 files changed, 786 insertions, 0 deletions
diff --git a/libkeyring/NOTICE b/libkeyring/NOTICE new file mode 100644 index 00000000..e8c19e7f --- /dev/null +++ b/libkeyring/NOTICE @@ -0,0 +1,25 @@ +This copyright NOTICE applies to all files in this directory and +subdirectories, unless another copyright notice appears in a given +file or subdirectory. If you take substantial code from this software to use in +other programs, you must somehow include with it an appropriate +copyright notice that includes the copyright notice and the other +notices below. It is fine (and often tidier) to do that in a separate +file such as NOTICE, LICENCE or COPYING. + +Copyright © 1995-1999 Lucent Technologies Inc. +Portions Copyright © 1997-2000 Vita Nuova Limited +Portions Copyright © 2000-2006 Vita Nuova Holdings Limited + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License (`LGPL') as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. diff --git a/libkeyring/dsaalg.c b/libkeyring/dsaalg.c new file mode 100644 index 00000000..f94647bd --- /dev/null +++ b/libkeyring/dsaalg.c @@ -0,0 +1,203 @@ +#include <lib9.h> +#include <kernel.h> +#include <isa.h> +#include "interp.h" +#include "../libinterp/runt.h" +#include "mp.h" +#include "libsec.h" +#include "keys.h" + +static char* pkattr[] = { "p", "q", "alpha", "key", nil }; +static char* skattr[] = { "p", "q", "alpha", "key", "!secret", nil }; +static char* sigattr[] = { "r", "s", nil }; + +static void* +dsa_str2sk(char *str, char **strp) +{ + DSApriv *dsa; + char *p; + + dsa = dsaprivalloc(); + dsa->pub.p = base64tobig(str, &p); + dsa->pub.q = base64tobig(str, &p); + dsa->pub.alpha = base64tobig(p, &p); + dsa->pub.key = base64tobig(p, &p); + dsa->secret = base64tobig(p, &p); + if(strp) + *strp = p; + return dsa; +} + +static void* +dsa_str2pk(char *str, char **strp) +{ + DSApub *dsa; + char *p; + + dsa = dsapuballoc(); + dsa->p = base64tobig(str, &p); + dsa->q = base64tobig(str, &p); + dsa->alpha = base64tobig(p, &p); + dsa->key = base64tobig(p, &p); + if(strp) + *strp = p; + return dsa; +} + +static void* +dsa_str2sig(char *str, char **strp) +{ + DSAsig *dsa; + char *p; + + dsa = dsasigalloc(); + dsa->r = base64tobig(str, &p); + dsa->s = base64tobig(p, &p); + if(strp) + *strp = p; + return dsa; +} + +static int +dsa_sk2str(void *veg, char *buf, int len) +{ + DSApriv *dsa; + char *cp, *ep; + + dsa = veg; + ep = buf + len - 1; + cp = buf; + + cp += snprint(cp, ep - cp, "%U\n", dsa->pub.p); + cp += snprint(cp, ep - cp, "%U\n", dsa->pub.q); + cp += snprint(cp, ep - cp, "%U\n", dsa->pub.alpha); + cp += snprint(cp, ep - cp, "%U\n", dsa->pub.key); + cp += snprint(cp, ep - cp, "%U\n", dsa->secret); + *cp = 0; + + return cp - buf; +} + +static int +dsa_pk2str(void *veg, char *buf, int len) +{ + DSApub *dsa; + char *cp, *ep; + + dsa = veg; + ep = buf + len - 1; + cp = buf; + + cp += snprint(cp, ep - cp, "%U\n", dsa->p); + cp += snprint(cp, ep - cp, "%U\n", dsa->q); + cp += snprint(cp, ep - cp, "%U\n", dsa->alpha); + cp += snprint(cp, ep - cp, "%U\n", dsa->key); + *cp = 0; + + return cp - buf; +} + +static int +dsa_sig2str(void *veg, char *buf, int len) +{ + DSAsig *dsa; + char *cp, *ep; + + dsa = veg; + ep = buf + len - 1; + cp = buf; + + cp += snprint(cp, ep - cp, "%U\n", dsa->r); + cp += snprint(cp, ep - cp, "%U\n", dsa->s); + *cp = 0; + + return cp - buf; +} + +static void* +dsa_sk2pk(void *vs) +{ + return dsaprivtopub((DSApriv*)vs); +} + +/* generate a dsa secret key with new params */ +static void* +dsa_gen(int len) +{ + USED(len); + return dsagen(nil); +} + +/* generate a dsa secret key with same params as a public key */ +static void* +dsa_genfrompk(void *vpub) +{ + return dsagen((DSApub*)vpub); +} + +static void +dsa_freepub(void *a) +{ + dsapubfree((DSApub*)a); +} + +static void +dsa_freepriv(void *a) +{ + dsaprivfree((DSApriv*)a); +} + +static void +dsa_freesig(void *a) +{ + dsasigfree((DSAsig*)a); +} + +static void* +dsa_sign(BigInt md, void *key) +{ + return dsasign((DSApriv*)key, md); +} + +static int +dsa_verify(BigInt md, void *sig, void *key) +{ + return dsaverify((DSApub*)key, (DSAsig*)sig, md) == 0; +} + +SigAlgVec* +dsainit(void) +{ + SigAlgVec *vec; + + vec = malloc(sizeof(SigAlgVec)); + if(vec == nil) + return nil; + + vec->name = "dsa"; + + vec->pkattr = pkattr; + vec->skattr = skattr; + vec->sigattr = sigattr; + + vec->str2sk = dsa_str2sk; + vec->str2pk = dsa_str2pk; + vec->str2sig = dsa_str2sig; + + vec->sk2str = dsa_sk2str; + vec->pk2str = dsa_pk2str; + vec->sig2str = dsa_sig2str; + + vec->sk2pk = dsa_sk2pk; + + vec->gensk = dsa_gen; + vec->genskfrompk = dsa_genfrompk; + vec->sign = dsa_sign; + vec->verify = dsa_verify; + + vec->skfree = dsa_freepriv; + vec->pkfree = dsa_freepub; + vec->sigfree = dsa_freesig; + + return vec; +} diff --git a/libkeyring/egalg.c b/libkeyring/egalg.c new file mode 100644 index 00000000..98567c95 --- /dev/null +++ b/libkeyring/egalg.c @@ -0,0 +1,212 @@ +#include <lib9.h> +#include <kernel.h> +#include <isa.h> +#include "interp.h" +#include "../libinterp/runt.h" +#include "mp.h" +#include "libsec.h" +#include "keys.h" + +static char* pkattr[] = { "p", "alpha", "key", nil }; +static char* skattr[] = { "p", "alpha", "key", "!secret", nil }; +static char* sigattr[] = { "r", "s", nil }; + +static void* +eg_str2sk(char *str, char **strp) +{ + EGpriv *eg; + char *p; + + eg = egprivalloc(); + eg->pub.p = base64tobig(str, &p); + eg->pub.alpha = base64tobig(p, &p); + eg->pub.key = base64tobig(p, &p); + eg->secret = base64tobig(p, &p); + if(strp) + *strp = p; + return eg; +} + +static void* +eg_str2pk(char *str, char **strp) +{ + EGpub *eg; + char *p; + + eg = egpuballoc(); + eg->p = base64tobig(str, &p); + eg->alpha = base64tobig(p, &p); + eg->key = base64tobig(p, &p); + if(strp) + *strp = p; + return eg; +} + +static void* +eg_str2sig(char *str, char **strp) +{ + EGsig *eg; + char *p; + + eg = egsigalloc(); + eg->r = base64tobig(str, &p); + eg->s = base64tobig(p, &p); + if(strp) + *strp = p; + return eg; +} + +static int +eg_sk2str(void *veg, char *buf, int len) +{ + EGpriv *eg; + char *cp, *ep; + + eg = (EGpriv*)veg; + ep = buf + len - 1; + cp = buf; + + cp += snprint(cp, ep - cp, "%U\n", eg->pub.p); + cp += snprint(cp, ep - cp, "%U\n", eg->pub.alpha); + cp += snprint(cp, ep - cp, "%U\n", eg->pub.key); + cp += snprint(cp, ep - cp, "%U\n", eg->secret); + *cp = 0; + + return cp - buf; +} + +static int +eg_pk2str(void *veg, char *buf, int len) +{ + EGpub *eg; + char *cp, *ep; + + eg = (EGpub*)veg; + ep = buf + len - 1; + cp = buf; + + cp += snprint(cp, ep - cp, "%U\n", eg->p); + cp += snprint(cp, ep - cp, "%U\n", eg->alpha); + cp += snprint(cp, ep - cp, "%U\n", eg->key); + *cp = 0; + + return cp - buf; +} + +static int +eg_sig2str(void *veg, char *buf, int len) +{ + EGsig *eg; + char *cp, *ep; + + eg = veg; + ep = buf + len - 1; + cp = buf; + + cp += snprint(cp, ep - cp, "%U\n", eg->r); + cp += snprint(cp, ep - cp, "%U\n", eg->s); + *cp = 0; + + return cp - buf; +} + +static void* +eg_sk2pk(void *vs) +{ + return egprivtopub((EGpriv*)vs); +} + +/* generate an el gamal secret key with new params */ +static void* +eg_gen(int len) +{ + return eggen(len, 0); +} + +/* generate an el gamal secret key with same params as a public key */ +static void* +eg_genfrompk(void *vpub) +{ + EGpub *pub; + EGpriv *priv; + int nlen; + + pub = vpub; + priv = egprivalloc(); + priv->pub.p = mpcopy(pub->p); + priv->pub.alpha = mpcopy(pub->alpha); + nlen = mpsignif(pub->p); + pub = &priv->pub; + pub->key = mpnew(0); + priv->secret = mpnew(0); + mprand(nlen-1, genrandom, priv->secret); + mpexp(pub->alpha, priv->secret, pub->p, pub->key); + return priv; +} + +static void* +eg_sign(BigInt mp, void *key) +{ + return egsign((EGpriv*)key, mp); +} + +static int +eg_verify(BigInt mp, void *sig, void *key) +{ + return egverify((EGpub*)key, (EGsig*)sig, mp) == 0; +} + +static void +eg_freepub(void *a) +{ + egpubfree((EGpub*)a); +} + +static void +eg_freepriv(void *a) +{ + egprivfree((EGpriv*)a); +} + +static void +eg_freesig(void *a) +{ + egsigfree((EGsig*)a); +} + +SigAlgVec* +elgamalinit(void) +{ + SigAlgVec *vec; + + vec = malloc(sizeof(SigAlgVec)); + if(vec == nil) + return nil; + + vec->name = "elgamal"; + + vec->pkattr = pkattr; + vec->skattr = skattr; + vec->sigattr = sigattr; + + vec->str2sk = eg_str2sk; + vec->str2pk = eg_str2pk; + vec->str2sig = eg_str2sig; + + vec->sk2str = eg_sk2str; + vec->pk2str = eg_pk2str; + vec->sig2str = eg_sig2str; + + vec->sk2pk = eg_sk2pk; + + vec->gensk = eg_gen; + vec->genskfrompk = eg_genfrompk; + vec->sign = eg_sign; + vec->verify = eg_verify; + + vec->skfree = eg_freepriv; + vec->pkfree = eg_freepub; + vec->sigfree = eg_freesig; + + return vec; +} diff --git a/libkeyring/keys.h b/libkeyring/keys.h new file mode 100644 index 00000000..fc57fd5c --- /dev/null +++ b/libkeyring/keys.h @@ -0,0 +1,118 @@ +typedef struct IPint IPint; +typedef struct SigAlg SigAlg; +typedef struct SigAlgVec SigAlgVec; +typedef struct SK SK; +typedef struct PK PK; +typedef struct Certificate Certificate; +typedef struct XDigestState XDigestState; +typedef struct XAESstate XAESstate; +typedef struct XDESstate XDESstate; +typedef struct XIDEAstate XIDEAstate; +typedef struct XRC4state XRC4state; + +enum +{ + Maxbuf= 4096, + MaxBigBytes = 1024 +}; + +/* infininite precision integer */ +struct IPint +{ + Keyring_IPint x; + BigInt b; +}; + +/* generic certificate */ +struct Certificate +{ + Keyring_Certificate x; + void *signa; /* actual signature */ +}; + +/* generic public key */ +struct PK +{ + Keyring_PK x; + void *key; /* key and system parameters */ +}; + +/* digest state */ +struct XDigestState +{ + Keyring_DigestState x; + DigestState state; +}; + +/* AES state */ +struct XAESstate +{ + Keyring_AESstate x; + AESstate state; +}; + +/* DES state */ +struct XDESstate +{ + Keyring_DESstate x; + DESstate state; +}; + +/* IDEA state */ +struct XIDEAstate +{ + Keyring_IDEAstate x; + IDEAstate state; +}; + +/* RC4 state */ +struct XRC4state +{ + Keyring_RC4state x; + RC4state state; +}; + +/* generic secret key */ +struct SK +{ + Keyring_SK x; + void *key; /* key and system parameters */ +}; + +struct SigAlgVec { + char *name; + + char** skattr; + char** pkattr; + char** sigattr; + + void* (*str2sk)(char*, char**); + void* (*str2pk)(char*, char**); + void* (*str2sig)(char*, char**); + + int (*sk2str)(void*, char*, int); + int (*pk2str)(void*, char*, int); + int (*sig2str)(void*, char*, int); + + void* (*sk2pk)(void*); + + void* (*gensk)(int); + void* (*genskfrompk)(void*); + void* (*sign)(BigInt, void*); + int (*verify)(BigInt, void*, void*); + + void (*skfree)(void*); + void (*pkfree)(void*); + void (*sigfree)(void*); +}; + +struct SigAlg +{ + Keyring_SigAlg x; + SigAlgVec *vec; +}; + +int bigtobase64(BigInt b, char *buf, int blen); +BigInt base64tobig(char *str, char **strp); +SigAlgVec* findsigalg(char*); +Keyring_IPint* newIPint(BigInt); diff --git a/libkeyring/mkfile b/libkeyring/mkfile new file mode 100644 index 00000000..3eec04a9 --- /dev/null +++ b/libkeyring/mkfile @@ -0,0 +1,17 @@ +<../mkconfig + +LIB=libkeyring.a + +OFILES=\ + dsaalg.$O\ + egalg.$O\ + rsaalg.$O + +HFILES=\ + $ROOT/include/mp.h\ + $ROOT/include/libsec.h\ + $ROOT/libinterp/runt.h\ + $ROOT/include/interp.h\ + keys.h\ + +<$ROOT/mkfiles/mksyslib-$SHELLTYPE diff --git a/libkeyring/rsaalg.c b/libkeyring/rsaalg.c new file mode 100644 index 00000000..627acb95 --- /dev/null +++ b/libkeyring/rsaalg.c @@ -0,0 +1,211 @@ +#include <lib9.h> +#include <kernel.h> +#include <isa.h> +#include "interp.h" +#include "../libinterp/runt.h" +#include "mp.h" +#include "libsec.h" +#include "keys.h" + +static char* pkattr[] = { "n", "ek", nil }; +static char* skattr[] = { "n", "ek", "!dk", "!p", "!q", "!kp", "!kq", "!c2", nil }; +static char* sigattr[] = { "val", nil }; + +static void* +rsa_str2sk(char *str, char **strp) +{ + RSApriv *rsa; + char *p; + + rsa = rsaprivalloc(); + rsa->pub.n = base64tobig(str, &p); + rsa->pub.ek = base64tobig(p, &p); + rsa->dk = base64tobig(p, &p); + rsa->p = base64tobig(p, &p); + rsa->q = base64tobig(p, &p); + rsa->kp = base64tobig(p, &p); + rsa->kq = base64tobig(p, &p); + rsa->c2 = base64tobig(p, &p); + if(strp) + *strp = p; + + return rsa; +} + +static void* +rsa_str2pk(char *str, char **strp) +{ + RSApub *rsa; + char *p; + + rsa = rsapuballoc(); + rsa->n = base64tobig(str, &p); + rsa->ek = base64tobig(p, &p); + if(strp) + *strp = p; + + return rsa; +} + +static void* +rsa_str2sig(char *str, char **strp) +{ + BigInt rsa; + char *p; + + rsa = base64tobig(str, &p); + if(strp) + *strp = p; + return rsa; +} + +static int +rsa_sk2str(void *vrsa, char *buf, int len) +{ + RSApriv *rsa; + char *cp, *ep; + + rsa = vrsa; + ep = buf + len - 1; + cp = buf; + + cp += snprint(cp, ep - cp, "%U\n", rsa->pub.n); + cp += snprint(cp, ep - cp, "%U\n", rsa->pub.ek); + cp += snprint(cp, ep - cp, "%U\n", rsa->dk); + cp += snprint(cp, ep - cp, "%U\n", rsa->p); + cp += snprint(cp, ep - cp, "%U\n", rsa->q); + cp += snprint(cp, ep - cp, "%U\n", rsa->kp); + cp += snprint(cp, ep - cp, "%U\n", rsa->kq); + cp += snprint(cp, ep - cp, "%U\n", rsa->c2); + *cp = 0; + + return cp - buf; +} + +static int +rsa_pk2str(void *vrsa, char *buf, int len) +{ + RSApub *rsa; + char *cp, *ep; + + rsa = vrsa; + ep = buf + len - 1; + cp = buf; + cp += snprint(cp, ep - cp, "%U\n", rsa->n); + cp += snprint(cp, ep - cp, "%U\n", rsa->ek); + *cp = 0; + + return cp - buf; +} + +static int +rsa_sig2str(void *vrsa, char *buf, int len) +{ + BigInt rsa; + char *cp, *ep; + + rsa = vrsa; + ep = buf + len - 1; + cp = buf; + + cp += snprint(cp, ep - cp, "%U\n", rsa); + *cp = 0; + + return cp - buf; +} + +static void* +rsa_sk2pk(void *vs) +{ + return rsaprivtopub((RSApriv*)vs); +} + +/* generate an rsa secret key */ +static void* +rsa_gen(int len) +{ + return rsagen(len, 8, 0); +} + +/* generate an rsa secret key with same params as a public key */ +static void* +rsa_genfrompk(void *vpub) +{ + RSApub *pub; + + pub = vpub; + return rsagen(mpsignif(pub->n), mpsignif(pub->ek), 0); +} + +static void* +rsa_sign(BigInt m, void *key) +{ + return rsadecrypt((RSApriv*)key, m, nil); +} + +static int +rsa_verify(BigInt m, void *sig, void *key) +{ + BigInt t; + int r; + + t = rsaencrypt((RSApub*)key, (BigInt)sig, nil); + r = mpcmp(t, m) == 0; + mpfree(t); + return r; +} + +static void +rsa_freepriv(void *a) +{ + rsaprivfree((RSApriv*)a); +} + +static void +rsa_freepub(void *a) +{ + rsapubfree((RSApub*)a); +} + +static void +rsa_freesig(void *a) +{ + mpfree((BigInt)a); +} + +SigAlgVec* +rsainit(void) +{ + SigAlgVec *vec; + + vec = malloc(sizeof(SigAlgVec)); + if(vec == nil) + return nil; + + vec->name = "rsa"; + + vec->pkattr = pkattr; + vec->skattr = skattr; + vec->sigattr = sigattr; + + vec->str2sk = rsa_str2sk; + vec->str2pk = rsa_str2pk; + vec->str2sig = rsa_str2sig; + + vec->sk2str = rsa_sk2str; + vec->pk2str = rsa_pk2str; + vec->sig2str = rsa_sig2str; + + vec->sk2pk = rsa_sk2pk; + + vec->gensk = rsa_gen; + vec->genskfrompk = rsa_genfrompk; + vec->sign = rsa_sign; + vec->verify = rsa_verify; + + vec->skfree = rsa_freepriv; + vec->pkfree = rsa_freepub; + vec->sigfree = rsa_freesig; + + return vec; +} |
