]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/include/libsec.h
ndb/dnstcp: -x specifies the mountmoint
[plan9front.git] / sys / include / libsec.h
index e3bbc7693113f7e4d3b837be25de4c5af0b98352..86cb342e5e35850a7fea6dd4a601d5242623d8ca 100644 (file)
@@ -23,7 +23,6 @@ struct AESstate
        ulong   setup;
        int     rounds;
        int     keybytes;
-       uint    ctrsz;
        uchar   key[AESmaxkey];                 /* unexpanded key */
        ulong   ekey[4*(AESmaxrounds + 1)];     /* encryption key */
        ulong   dkey[4*(AESmaxrounds + 1)];     /* decryption key */
@@ -38,8 +37,6 @@ void  aes_decrypt(ulong rk[], int Nr, uchar ct[16], uchar pt[16]);
 void   setupAESstate(AESstate *s, uchar key[], int keybytes, uchar *ivec);
 void   aesCBCencrypt(uchar *p, int len, AESstate *s);
 void   aesCBCdecrypt(uchar *p, int len, AESstate *s);
-void   aesCTRdecrypt(uchar *p, int len, AESstate *s);
-void   aesCTRencrypt(uchar *p, int len, AESstate *s);
 
 void   setupAESXCBCstate(AESstate *s);
 uchar* aesXCBCmac(uchar *p, int len, AESstate *s);
@@ -73,6 +70,42 @@ void bfCBCdecrypt(uchar*, int, BFstate*);
 void   bfECBencrypt(uchar*, int, BFstate*);
 void   bfECBdecrypt(uchar*, int, BFstate*);
 
+/*
+ * Chacha definitions
+ */
+
+enum
+{
+       ChachaBsize=    64,
+       ChachaKeylen=   256/8,
+       ChachaIVlen=    96/8,
+};
+
+typedef struct Chachastate Chachastate;
+struct Chachastate
+{
+       union{
+               u32int  input[16];
+               struct {
+                       u32int  constant[4];
+                       u32int  key[8];
+                       u32int  counter;
+                       u32int  iv[3];
+               };
+       };
+       int     rounds;
+       int     ivwords;
+};
+
+void   setupChachastate(Chachastate*, uchar*, ulong, uchar*, ulong, int);
+void   chacha_setiv(Chachastate *, uchar*);
+void   chacha_setblock(Chachastate*, u64int);
+void   chacha_encrypt(uchar*, ulong, Chachastate*);
+void   chacha_encrypt2(uchar*, uchar*, ulong, Chachastate*);
+
+void   ccpoly_encrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], Chachastate *cs);
+int    ccpoly_decrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], Chachastate *cs);
+
 /*
  * DES definitions
  */
@@ -144,7 +177,7 @@ enum
        SHA2_512dlen=   64,     /* SHA-512 digest length */
        MD4dlen=        16,     /* MD4 digest length */
        MD5dlen=        16,     /* MD5 digest length */
-       AESdlen=        16,     /* TODO: see rfc */
+       Poly1305dlen=   16,     /* Poly1305 digest length */
 
        Hmacblksz       = 64,   /* in bytes; from rfc2104 */
 };
@@ -170,7 +203,6 @@ typedef struct DigestState SHA2_384state;
 typedef struct DigestState SHA2_512state;
 typedef struct DigestState MD5state;
 typedef struct DigestState MD4state;
-typedef struct DigestState AEShstate;
 
 DigestState*   md4(uchar*, ulong, uchar*, DigestState*);
 DigestState*   md5(uchar*, ulong, uchar*, DigestState*);
@@ -179,7 +211,6 @@ DigestState*        sha2_224(uchar*, ulong, uchar*, DigestState*);
 DigestState*   sha2_256(uchar*, ulong, uchar*, DigestState*);
 DigestState*   sha2_384(uchar*, ulong, uchar*, DigestState*);
 DigestState*   sha2_512(uchar*, ulong, uchar*, DigestState*);
-DigestState*   aes(uchar*, ulong, uchar*, DigestState*);
 DigestState*   hmac_x(uchar *p, ulong len, uchar *key, ulong klen,
                        uchar *digest, DigestState *s,
                        DigestState*(*x)(uchar*, ulong, uchar*, DigestState*),
@@ -190,12 +221,13 @@ DigestState*      hmac_sha2_224(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
 DigestState*   hmac_sha2_256(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
 DigestState*   hmac_sha2_384(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
 DigestState*   hmac_sha2_512(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
-DigestState*   hmac_aes(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
 char*          md5pickle(MD5state*);
 MD5state*      md5unpickle(char*);
 char*          sha1pickle(SHA1state*);
 SHA1state*     sha1unpickle(char*);
 
+DigestState*   poly1305(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
+
 /*
  * random number generation
  */
@@ -281,6 +313,8 @@ uchar*              decodePEM(char *s, char *type, int *len, char **new_s);
 PEMChain*      decodepemchain(char *s, char *type);
 uchar*         X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
 uchar*         X509req(RSApriv *priv, char *subj, int *certlen);
+char*          X509verifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, RSApub *pk);
+char*          X509verifydata(uchar *sig, int siglen, uchar *data, int datalen, RSApub *pk);
 char*          X509verify(uchar *cert, int ncert, RSApub *pk);
 void           X509dump(uchar *cert, int ncert);
 
@@ -378,14 +412,18 @@ typedef struct TLSconn{
        char    dir[40];        /* connection directory */
        uchar   *cert;  /* certificate (local on input, remote on output) */
        uchar   *sessionID;
+       uchar   *psk;
        int     certlen;
        int     sessionIDlen;
+       int     psklen;
        int     (*trace)(char*fmt, ...);
        PEMChain*chain; /* optional extra certificate evidence for servers to present */
        char    *sessionType;
        uchar   *sessionKey;
        int     sessionKeylen;
        char    *sessionConst;
+       char    *serverName;
+       char    *pskID;
 } TLSconn;
 
 /* tlshand.c */
@@ -400,3 +438,80 @@ int        okThumbprint(uchar *sha1, Thumbprint *ok);
 /* readcert.c */
 uchar  *readcert(char *filename, int *pcertlen);
 PEMChain*readcertchain(char *filename);
+
+/* aes_xts.c */
+int aes_xts_encrypt(ulong tweak[], ulong ecb[],  vlong sectorNumber, uchar *input, uchar *output, ulong len) ;
+int aes_xts_decrypt(ulong tweak[], ulong ecb[], vlong sectorNumber, uchar *input, uchar *output, ulong len);
+
+typedef struct ECpoint{
+       int inf;
+       mpint *x;
+       mpint *y;
+} ECpoint;
+
+typedef ECpoint ECpub;
+typedef struct ECpriv{
+       ECpoint;
+       mpint *d;
+} ECpriv;
+
+typedef struct ECdomain{
+       mpint *p;
+       mpint *a;
+       mpint *b;
+       ECpoint *G;
+       mpint *n;
+       mpint *h;
+} ECdomain;
+
+void   ecassign(ECdomain *, ECpoint *old, ECpoint *new);
+void   ecadd(ECdomain *, ECpoint *a, ECpoint *b, ECpoint *s);
+void   ecmul(ECdomain *, ECpoint *a, mpint *k, ECpoint *s);
+ECpoint*       strtoec(ECdomain *, char *, char **, ECpoint *);
+ECpriv*        ecgen(ECdomain *, ECpriv*);
+int    ecverify(ECdomain *, ECpoint *);
+int    ecpubverify(ECdomain *, ECpub *);
+void   ecdsasign(ECdomain *, ECpriv *, uchar *, int, mpint *, mpint *);
+int    ecdsaverify(ECdomain *, ECpub *, uchar *, int, mpint *, mpint *);
+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   *q;     /* subgroup 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 *q, mpint *g);
+
+/* calculate shared key: k = y**x % p */
+mpint* dh_finish(DHstate *dh, mpint *y);
+
+/* Curve25519 elliptic curve, public key function */
+void curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]);
+
+/* Curve25519 diffie hellman */
+void curve25519_dh_new(uchar x[32], uchar y[32]);
+void curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]);
+
+/* password-based key derivation function 2 (rfc2898) */
+void pbkdf2_x(uchar *p, ulong plen, uchar *s, ulong slen, ulong rounds, uchar *d, ulong dlen,
+       DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
+
+/* hmac-based key derivation function (rfc5869) */
+void hkdf_x(uchar *salt, ulong nsalt, uchar *info, ulong ninfo, uchar *key, ulong nkey, uchar *d, ulong dlen,
+       DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
+
+/* timing safe memcmp() */
+int tsmemcmp(void*, void*, ulong);