From b786f20bbab5a59046aa78a2c6c2a11536497202 Mon Sep 17 00:00:00 2001 From: bhgv Date: Thu, 1 Mar 2018 16:54:45 +0200 Subject: inferno-os tree was separated from the inferno-os-android (separated from the Android driver) --- libkeyring/NOTICE | 25 ++++++ libkeyring/dsaalg.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++ libkeyring/egalg.c | 224 ++++++++++++++++++++++++++++++++++++++++++++++++++ libkeyring/keys.h | 111 +++++++++++++++++++++++++ libkeyring/mkfile | 17 ++++ libkeyring/rsaalg.c | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 823 insertions(+) create mode 100644 libkeyring/NOTICE create mode 100644 libkeyring/dsaalg.c create mode 100644 libkeyring/egalg.c create mode 100644 libkeyring/keys.h create mode 100644 libkeyring/mkfile create mode 100644 libkeyring/rsaalg.c (limited to 'libkeyring') diff --git a/libkeyring/NOTICE b/libkeyring/NOTICE new file mode 100644 index 0000000..a9a7616 --- /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-2007 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 0000000..2bf659f --- /dev/null +++ b/libkeyring/dsaalg.c @@ -0,0 +1,216 @@ +#include +#include +#include +#include "interp.h" +#include "../libinterp/keyringif.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; + if(dsa->pub.p == nil || dsa->pub.q == nil || + dsa->pub.alpha == nil || dsa->pub.key == nil || dsa->secret == nil){ + dsaprivfree(dsa); + return nil; + } + 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; + if(dsa->p == nil || dsa->q == nil || dsa->alpha == nil || dsa->key == nil){ + dsapubfree(dsa); + return nil; + } + 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; + if(dsa->r == nil || dsa->s == nil){ + dsasigfree(dsa); + return nil; + } + 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(mpint* md, void *key) +{ + return dsasign((DSApriv*)key, md); +} + +static int +dsa_verify(mpint* 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 0000000..28202c1 --- /dev/null +++ b/libkeyring/egalg.c @@ -0,0 +1,224 @@ +#include +#include +#include +#include "interp.h" +#include "../libinterp/keyringif.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; + if(eg->pub.p == nil || eg->pub.alpha == nil || eg->pub.key == nil || eg->secret == nil){ + egprivfree(eg); + return nil; + } + 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; + if(eg->p == nil || eg->alpha == nil || eg->key == nil){ + egpubfree(eg); + return nil; + } + 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; + if(eg->r == nil || eg->s == nil){ + egsigfree(eg); + return nil; + } + 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(mpint* mp, void *key) +{ + return egsign((EGpriv*)key, mp); +} + +static int +eg_verify(mpint* 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 0000000..4071ca0 --- /dev/null +++ b/libkeyring/keys.h @@ -0,0 +1,111 @@ +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 +}; + +/* 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)(mpint*, void*); + int (*verify)(mpint*, void*, void*); + + void (*skfree)(void*); + void (*pkfree)(void*); + void (*sigfree)(void*); +}; + +struct SigAlg +{ + Keyring_SigAlg x; + SigAlgVec *vec; +}; + +int bigtobase64(mpint* b, char *buf, int blen); +mpint* base64tobig(char *str, char **strp); +SigAlgVec* findsigalg(char*); +//Keyring_IPint* newIPint(mpint*); +void* newIPint(mpint*); diff --git a/libkeyring/mkfile b/libkeyring/mkfile new file mode 100644 index 0000000..3eec04a --- /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 0000000..46df7e5 --- /dev/null +++ b/libkeyring/rsaalg.c @@ -0,0 +1,230 @@ +#include +#include +#include +#include "interp.h" +#include "../libinterp/keyringif.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; + if(rsa->pub.n == nil || rsa->pub.ek == nil || + rsa->dk == nil || rsa->p == nil || rsa->q == nil || + rsa->kp == nil || rsa->kq == nil || rsa->c2 == nil){ + rsaprivfree(rsa); + return nil; + } + + 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; + if(rsa->n == nil || rsa->ek == nil){ + rsapubfree(rsa); + return nil; + } + + return rsa; +} + +static void* +rsa_str2sig(char *str, char **strp) +{ + mpint *rsa; + char *p; + + rsa = base64tobig(str, &p); + if(rsa == nil) + return nil; + 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) +{ + mpint *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) +{ + RSApriv *key; + + for(;;){ + key = rsagen(len, 6, 0); + if(mpsignif(key->pub.n) == len) + return key; + rsaprivfree(key); + } +} + +/* 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(mpint* m, void *key) +{ + return rsadecrypt((RSApriv*)key, m, nil); +} + +static int +rsa_verify(mpint* m, void *sig, void *key) +{ + mpint *t; + int r; + + t = rsaencrypt((RSApub*)key, (mpint*)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(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; +} -- cgit v1.2.3