#pragma lib "libsec.a"#pragma src "/sys/src/libsec" #ifndef _MPINTtypedef struct mpint mpint;#endif /////////////////////////////////////////////////////////// DES definitions///////////////////////////////////////////////////////// enum{ DESbsize= 8}; // single destypedef struct DESstate DESstate;struct DESstate{ ulong setup; uchar key[8]; /* unexpanded key */ ulong expanded[32]; /* expanded key */ uchar ivec[8]; /* initialization vector */}; void setupDESstate(DESstate *s, uchar key[8], uchar *ivec);void des_key_setup(uchar[8], ulong[32]);void block_cipher(ulong*, uchar*, int);void desCBCencrypt(uchar*, int, DESstate*);void desCBCdecrypt(uchar*, int, DESstate*);void desECBencrypt(uchar*, int, DESstate*);void desECBdecrypt(uchar*, int, DESstate*); // for backward compatibility with 7 byte DES key formatvoid des56to64(uchar *k56, uchar *k64);void des64to56(uchar *k64, uchar *k56);void key_setup(uchar[7], ulong[32]); // triple des encrypt/decrypt orderingsenum { DES3E= 0, DES3D= 1, DES3EEE= 0, DES3EDE= 2, DES3DED= 5, DES3DDD= 7}; typedef struct DES3state DES3state;struct DES3state{ ulong setup; uchar key[3][8]; /* unexpanded key */ ulong expanded[3][32]; /* expanded key */ uchar ivec[8]; /* initialization vector */}; void setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec);void triple_block_cipher(ulong keys[3][32], uchar*, int);void des3CBCencrypt(uchar*, int, DES3state*);void des3CBCdecrypt(uchar*, int, DES3state*);void des3ECBencrypt(uchar*, int, DES3state*);void des3ECBdecrypt(uchar*, int, DES3state*); /////////////////////////////////////////////////////////// digests///////////////////////////////////////////////////////// enum{ SHA1dlen= 20, /* SHA digest length */ MD4dlen= 16, /* MD4 digest length */ MD5dlen= 16 /* MD5 digest length */}; typedef struct DigestState DigestState;struct DigestState{ ulong len; u32int state[5]; uchar buf[128]; int blen; char malloced; char seeded;};typedef struct DigestState SHAstate; /* obsolete name */typedef struct DigestState SHA1state;typedef struct DigestState MD5state;typedef struct DigestState MD4state; DigestState* md4(uchar*, ulong, uchar*, DigestState*);DigestState* md5(uchar*, ulong, uchar*, DigestState*);DigestState* sha1(uchar*, ulong, uchar*, DigestState*);DigestState* hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);DigestState* hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*); /////////////////////////////////////////////////////////// base 64 & 32 conversions///////////////////////////////////////////////////////// int dec64(uchar *out, int lim, char *in, int n);int enc64(char *out, int lim, uchar *in, int n);int dec32(uchar *out, int lim, char *in, int n);int enc32(char *out, int lim, uchar *in, int n); /////////////////////////////////////////////////////////// random number generation/////////////////////////////////////////////////////////void genrandom(uchar *buf, int nbytes);void prng(uchar *buf, int nbytes); /////////////////////////////////////////////////////////// primes/////////////////////////////////////////////////////////void genprime(mpint *p, int n, int accuracy); // generate an n bit probable primevoid gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); // prime and generatorvoid genstrongprime(mpint *p, int n, int accuracy); // generate an n bit strong primevoid DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);int probably_prime(mpint *n, int nrep); // miller-rabin testint smallprimetest(mpint *p); // returns -1 if not prime, 0 otherwise /////////////////////////////////////////////////////////// rc4/////////////////////////////////////////////////////////typedef struct RC4state RC4state;struct RC4state{ uchar state[256]; uchar x; uchar y;}; void setupRC4state(RC4state*, uchar*, int);void rc4(RC4state*, uchar*, int);void rc4skip(RC4state*, int);void rc4back(RC4state*, int); /////////////////////////////////////////////////////////// rsa/////////////////////////////////////////////////////////typedef struct RSApub RSApub;typedef struct RSApriv RSApriv; // public/encryption keystruct RSApub{ mpint *n; // modulus mpint *ek; // exp (encryption key)}; // private/decryption keystruct RSApriv{ RSApub pub; mpint *dk; // exp (decryption key) // precomputed values to help with chinese remainder theorem calc mpint *p; mpint *q; mpint *kp; // dk mod p-1 mpint *kq; // dk mod q-1 mpint *c2; // for converting modular rep to answer}; RSApriv* rsagen(int nlen, int elen, int rounds);mpint* rsaencrypt(RSApub *k, mpint *in, mpint *out);mpint* rsadecrypt(RSApriv *k, mpint *in, mpint *out);RSApub* rsapuballoc(void);void rsapubfree(RSApub*);RSApriv* rsaprivalloc(void);void rsaprivfree(RSApriv*);RSApub* rsaprivtopub(RSApriv*);RSApub* X509toRSApub(uchar*,int,char*,int); /////////////////////////////////////////////////////////// eg/////////////////////////////////////////////////////////typedef struct EGpub EGpub;typedef struct EGpriv EGpriv;typedef struct EGsig EGsig; // public/encryption keystruct EGpub{ mpint *p; // modulus mpint *alpha; // generator mpint *key; // (encryption key) alpha**secret mod p}; // private/decryption keystruct EGpriv{ EGpub pub; mpint *secret; // (decryption key)}; // signaturestruct EGsig{ mpint *r, *s;}; EGpriv* eggen(int nlen, int rounds);mpint* egencrypt(EGpub *k, mpint *in, mpint *out);mpint* egdecrypt(EGpriv *k, mpint *in, mpint *out);EGsig* egsign(EGpriv *k, mpint *m);int egverify(EGpub *k, EGsig *sig, mpint *m);EGpub* egpuballoc(void);void egpubfree(EGpub*);EGpriv* egprivalloc(void);void egprivfree(EGpriv*);EGsig* egsigalloc(void);void egsigfree(EGsig*);EGpub* egprivtopub(EGpriv*);
#pragma lib "libsec.a"
#pragma src "/sys/src/libsec"
#ifndef _MPINT
typedef struct mpint mpint;
#endif
/////////////////////////////////////////////////////////
// DES definitions
/////////////////////////////////////////////////////////
enum
{
DESbsize= 8
};
// single des
typedef struct DESstate DESstate;
struct DESstate
{
ulong setup;
uchar key[8]; /* unexpanded key */
ulong expanded[32]; /* expanded key */
uchar ivec[8]; /* initialization vector */
};
void setupDESstate(DESstate *s, uchar key[8], uchar *ivec);
void des_key_setup(uchar[8], ulong[32]);
void block_cipher(ulong*, uchar*, int);
void desCBCencrypt(uchar*, int, DESstate*);
void desCBCdecrypt(uchar*, int, DESstate*);
void desECBencrypt(uchar*, int, DESstate*);
void desECBdecrypt(uchar*, int, DESstate*);
// for backward compatibility with 7 byte DES key format
void des56to64(uchar *k56, uchar *k64);
void des64to56(uchar *k64, uchar *k56);
void key_setup(uchar[7], ulong[32]);
// triple des encrypt/decrypt orderings
enum {
DES3E= 0,
DES3D= 1,
DES3EEE= 0,
DES3EDE= 2,
DES3DED= 5,
DES3DDD= 7
};
typedef struct DES3state DES3state;
struct DES3state
{
ulong setup;
uchar key[3][8]; /* unexpanded key */
ulong expanded[3][32]; /* expanded key */
uchar ivec[8]; /* initialization vector */
};
void setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec);
void triple_block_cipher(ulong keys[3][32], uchar*, int);
void des3CBCencrypt(uchar*, int, DES3state*);
void des3CBCdecrypt(uchar*, int, DES3state*);
void des3ECBencrypt(uchar*, int, DES3state*);
void des3ECBdecrypt(uchar*, int, DES3state*);
/////////////////////////////////////////////////////////
// digests
/////////////////////////////////////////////////////////
enum
{
SHA1dlen= 20, /* SHA digest length */
MD4dlen= 16, /* MD4 digest length */
MD5dlen= 16 /* MD5 digest length */
};
typedef struct DigestState DigestState;
struct DigestState
{
ulong len;
u32int state[5];
uchar buf[128];
int blen;
char malloced;
char seeded;
};
typedef struct DigestState SHAstate; /* obsolete name */
typedef struct DigestState SHA1state;
typedef struct DigestState MD5state;
typedef struct DigestState MD4state;
DigestState* md4(uchar*, ulong, uchar*, DigestState*);
DigestState* md5(uchar*, ulong, uchar*, DigestState*);
DigestState* sha1(uchar*, ulong, uchar*, DigestState*);
DigestState* hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
DigestState* hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
/////////////////////////////////////////////////////////
// base 64 & 32 conversions
/////////////////////////////////////////////////////////
int dec64(uchar *out, int lim, char *in, int n);
int enc64(char *out, int lim, uchar *in, int n);
int dec32(uchar *out, int lim, char *in, int n);
int enc32(char *out, int lim, uchar *in, int n);
/////////////////////////////////////////////////////////
// random number generation
/////////////////////////////////////////////////////////
void genrandom(uchar *buf, int nbytes);
void prng(uchar *buf, int nbytes);
/////////////////////////////////////////////////////////
// primes
/////////////////////////////////////////////////////////
void genprime(mpint *p, int n, int accuracy); // generate an n bit probable prime
void gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); // prime and generator
void genstrongprime(mpint *p, int n, int accuracy); // generate an n bit strong prime
void DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
int probably_prime(mpint *n, int nrep); // miller-rabin test
int smallprimetest(mpint *p); // returns -1 if not prime, 0 otherwise
/////////////////////////////////////////////////////////
// rc4
/////////////////////////////////////////////////////////
typedef struct RC4state RC4state;
struct RC4state
{
uchar state[256];
uchar x;
uchar y;
};
void setupRC4state(RC4state*, uchar*, int);
void rc4(RC4state*, uchar*, int);
void rc4skip(RC4state*, int);
void rc4back(RC4state*, int);
/////////////////////////////////////////////////////////
// rsa
/////////////////////////////////////////////////////////
typedef struct RSApub RSApub;
typedef struct RSApriv RSApriv;
// public/encryption key
struct RSApub
{
mpint *n; // modulus
mpint *ek; // exp (encryption key)
};
// private/decryption key
struct RSApriv
{
RSApub pub;
mpint *dk; // exp (decryption key)
// precomputed values to help with chinese remainder theorem calc
mpint *p;
mpint *q;
mpint *kp; // dk mod p-1
mpint *kq; // dk mod q-1
mpint *c2; // for converting modular rep to answer
};
RSApriv* rsagen(int nlen, int elen, int rounds);
mpint* rsaencrypt(RSApub *k, mpint *in, mpint *out);
mpint* rsadecrypt(RSApriv *k, mpint *in, mpint *out);
RSApub* rsapuballoc(void);
void rsapubfree(RSApub*);
RSApriv* rsaprivalloc(void);
void rsaprivfree(RSApriv*);
RSApub* rsaprivtopub(RSApriv*);
RSApub* X509toRSApub(uchar*,int,char*,int);
/////////////////////////////////////////////////////////
// eg
/////////////////////////////////////////////////////////
typedef struct EGpub EGpub;
typedef struct EGpriv EGpriv;
typedef struct EGsig EGsig;
// public/encryption key
struct EGpub
{
mpint *p; // modulus
mpint *alpha; // generator
mpint *key; // (encryption key) alpha**secret mod p
};
// private/decryption key
struct EGpriv
{
EGpub pub;
mpint *secret; // (decryption key)
};
// signature
struct EGsig
{
mpint *r, *s;
};
EGpriv* eggen(int nlen, int rounds);
mpint* egencrypt(EGpub *k, mpint *in, mpint *out);
mpint* egdecrypt(EGpriv *k, mpint *in, mpint *out);
EGsig* egsign(EGpriv *k, mpint *m);
int egverify(EGpub *k, EGsig *sig, mpint *m);
EGpub* egpuballoc(void);
void egpubfree(EGpub*);
EGpriv* egprivalloc(void);
void egprivfree(EGpriv*);
EGsig* egsigalloc(void);
void egsigfree(EGsig*);
EGpub* egprivtopub(EGpriv*);