]> git.lizzy.rs Git - plan9front.git/blob - sys/include/libsec.h
16f249f2e68fbcdc175b3c793bab7956e8ff2723
[plan9front.git] / sys / include / libsec.h
1 #pragma lib     "libsec.a"
2 #pragma src     "/sys/src/libsec"
3
4
5 #ifndef _MPINT
6 typedef struct mpint mpint;
7 #endif
8
9 /*
10  * AES definitions
11  */
12
13 enum
14 {
15         AESbsize=       16,
16         AESmaxkey=      32,
17         AESmaxrounds=   14
18 };
19
20 typedef struct AESstate AESstate;
21 struct AESstate
22 {
23         ulong   setup;
24         int     rounds;
25         int     keybytes;
26         uchar   key[AESmaxkey];                 /* unexpanded key */
27         ulong   ekey[4*(AESmaxrounds + 1)];     /* encryption key */
28         ulong   dkey[4*(AESmaxrounds + 1)];     /* decryption key */
29         uchar   ivec[AESbsize];                 /* initialization vector */
30         uchar   mackey[3 * AESbsize];           /* 3 XCBC mac 96 keys */
31 };
32
33 /* block ciphers */
34 void    aes_encrypt(ulong rk[], int Nr, uchar pt[16], uchar ct[16]);
35 void    aes_decrypt(ulong rk[], int Nr, uchar ct[16], uchar pt[16]);
36
37 void    setupAESstate(AESstate *s, uchar key[], int keybytes, uchar *ivec);
38 void    aesCBCencrypt(uchar *p, int len, AESstate *s);
39 void    aesCBCdecrypt(uchar *p, int len, AESstate *s);
40
41 void    setupAESXCBCstate(AESstate *s);
42 uchar*  aesXCBCmac(uchar *p, int len, AESstate *s);
43
44 /*
45  * Blowfish Definitions
46  */
47
48 enum
49 {
50         BFbsize = 8,
51         BFrounds= 16
52 };
53
54 /* 16-round Blowfish */
55 typedef struct BFstate BFstate;
56 struct BFstate
57 {
58         ulong   setup;
59
60         uchar   key[56];
61         uchar   ivec[8];
62
63         u32int  pbox[BFrounds+2];
64         u32int  sbox[1024];
65 };
66
67 void    setupBFstate(BFstate *s, uchar key[], int keybytes, uchar *ivec);
68 void    bfCBCencrypt(uchar*, int, BFstate*);
69 void    bfCBCdecrypt(uchar*, int, BFstate*);
70 void    bfECBencrypt(uchar*, int, BFstate*);
71 void    bfECBdecrypt(uchar*, int, BFstate*);
72
73 /*
74  * DES definitions
75  */
76
77 enum
78 {
79         DESbsize=       8
80 };
81
82 /* single des */
83 typedef struct DESstate DESstate;
84 struct DESstate
85 {
86         ulong   setup;
87         uchar   key[8];         /* unexpanded key */
88         ulong   expanded[32];   /* expanded key */
89         uchar   ivec[8];        /* initialization vector */
90 };
91
92 void    setupDESstate(DESstate *s, uchar key[8], uchar *ivec);
93 void    des_key_setup(uchar[8], ulong[32]);
94 void    block_cipher(ulong*, uchar*, int);
95 void    desCBCencrypt(uchar*, int, DESstate*);
96 void    desCBCdecrypt(uchar*, int, DESstate*);
97 void    desECBencrypt(uchar*, int, DESstate*);
98 void    desECBdecrypt(uchar*, int, DESstate*);
99
100 /* for backward compatibility with 7-byte DES key format */
101 void    des56to64(uchar *k56, uchar *k64);
102 void    des64to56(uchar *k64, uchar *k56);
103 void    key_setup(uchar[7], ulong[32]);
104
105 /* triple des encrypt/decrypt orderings */
106 enum {
107         DES3E=          0,
108         DES3D=          1,
109         DES3EEE=        0,
110         DES3EDE=        2,
111         DES3DED=        5,
112         DES3DDD=        7
113 };
114
115 typedef struct DES3state DES3state;
116 struct DES3state
117 {
118         ulong   setup;
119         uchar   key[3][8];              /* unexpanded key */
120         ulong   expanded[3][32];        /* expanded key */
121         uchar   ivec[8];                /* initialization vector */
122 };
123
124 void    setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec);
125 void    triple_block_cipher(ulong keys[3][32], uchar*, int);
126 void    des3CBCencrypt(uchar*, int, DES3state*);
127 void    des3CBCdecrypt(uchar*, int, DES3state*);
128 void    des3ECBencrypt(uchar*, int, DES3state*);
129 void    des3ECBdecrypt(uchar*, int, DES3state*);
130
131 /*
132  * digests
133  */
134
135 enum
136 {
137         SHA1dlen=       20,     /* SHA digest length */
138         SHA2_224dlen=   28,     /* SHA-224 digest length */
139         SHA2_256dlen=   32,     /* SHA-256 digest length */
140         SHA2_384dlen=   48,     /* SHA-384 digest length */
141         SHA2_512dlen=   64,     /* SHA-512 digest length */
142         MD4dlen=        16,     /* MD4 digest length */
143         MD5dlen=        16,     /* MD5 digest length */
144
145         Hmacblksz       = 64,   /* in bytes; from rfc2104 */
146 };
147
148 typedef struct DigestState DigestState;
149 struct DigestState
150 {
151         uvlong  len;
152         union {
153                 u32int  state[8];
154                 u64int  bstate[8];
155         };
156         uchar   buf[256];
157         int     blen;
158         char    malloced;
159         char    seeded;
160 };
161 typedef struct DigestState SHAstate;    /* obsolete name */
162 typedef struct DigestState SHA1state;
163 typedef struct DigestState SHA2_224state;
164 typedef struct DigestState SHA2_256state;
165 typedef struct DigestState SHA2_384state;
166 typedef struct DigestState SHA2_512state;
167 typedef struct DigestState MD5state;
168 typedef struct DigestState MD4state;
169
170 DigestState*    md4(uchar*, ulong, uchar*, DigestState*);
171 DigestState*    md5(uchar*, ulong, uchar*, DigestState*);
172 DigestState*    sha1(uchar*, ulong, uchar*, DigestState*);
173 DigestState*    sha2_224(uchar*, ulong, uchar*, DigestState*);
174 DigestState*    sha2_256(uchar*, ulong, uchar*, DigestState*);
175 DigestState*    sha2_384(uchar*, ulong, uchar*, DigestState*);
176 DigestState*    sha2_512(uchar*, ulong, uchar*, DigestState*);
177 DigestState*    hmac_x(uchar *p, ulong len, uchar *key, ulong klen,
178                         uchar *digest, DigestState *s,
179                         DigestState*(*x)(uchar*, ulong, uchar*, DigestState*),
180                         int xlen);
181 DigestState*    hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
182 DigestState*    hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
183 DigestState*    hmac_sha2_224(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
184 DigestState*    hmac_sha2_256(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
185 DigestState*    hmac_sha2_384(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
186 DigestState*    hmac_sha2_512(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
187 char*           md5pickle(MD5state*);
188 MD5state*       md5unpickle(char*);
189 char*           sha1pickle(SHA1state*);
190 SHA1state*      sha1unpickle(char*);
191
192 /*
193  * random number generation
194  */
195 void    genrandom(uchar *buf, int nbytes);
196 void    prng(uchar *buf, int nbytes);
197 ulong   fastrand(void);
198 ulong   nfastrand(ulong);
199
200 /*
201  * primes
202  */
203 void    genprime(mpint *p, int n, int accuracy); /* generate n-bit probable prime */
204 void    gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); /* prime & generator */
205 void    genstrongprime(mpint *p, int n, int accuracy); /* generate n-bit strong prime */
206 void    DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
207 int     probably_prime(mpint *n, int nrep);     /* miller-rabin test */
208 int     smallprimetest(mpint *p);  /* returns -1 if not prime, 0 otherwise */
209
210 /*
211  * rc4
212  */
213 typedef struct RC4state RC4state;
214 struct RC4state
215 {
216          uchar  state[256];
217          uchar  x;
218          uchar  y;
219 };
220
221 void    setupRC4state(RC4state*, uchar*, int);
222 void    rc4(RC4state*, uchar*, int);
223 void    rc4skip(RC4state*, int);
224 void    rc4back(RC4state*, int);
225
226 /*
227  * rsa
228  */
229 typedef struct RSApub RSApub;
230 typedef struct RSApriv RSApriv;
231 typedef struct PEMChain PEMChain;
232
233 /* public/encryption key */
234 struct RSApub
235 {
236         mpint   *n;     /* modulus */
237         mpint   *ek;    /* exp (encryption key) */
238 };
239
240 /* private/decryption key */
241 struct RSApriv
242 {
243         RSApub  pub;
244
245         mpint   *dk;    /* exp (decryption key) */
246
247         /* precomputed values to help with chinese remainder theorem calc */
248         mpint   *p;
249         mpint   *q;
250         mpint   *kp;    /* dk mod p-1 */
251         mpint   *kq;    /* dk mod q-1 */
252         mpint   *c2;    /* (inv p) mod q */
253 };
254
255 struct PEMChain{
256         PEMChain*next;
257         uchar   *pem;
258         int     pemlen;
259 };
260
261 RSApriv*        rsagen(int nlen, int elen, int rounds);
262 RSApriv*        rsafill(mpint *n, mpint *e, mpint *d, mpint *p, mpint *q);
263 mpint*          rsaencrypt(RSApub *k, mpint *in, mpint *out);
264 mpint*          rsadecrypt(RSApriv *k, mpint *in, mpint *out);
265 RSApub*         rsapuballoc(void);
266 void            rsapubfree(RSApub*);
267 RSApriv*        rsaprivalloc(void);
268 void            rsaprivfree(RSApriv*);
269 RSApub*         rsaprivtopub(RSApriv*);
270 RSApub*         X509toRSApub(uchar*, int, char*, int);
271 RSApriv*        asn1toRSApriv(uchar*, int);
272 void            asn1dump(uchar *der, int len);
273 uchar*          decodePEM(char *s, char *type, int *len, char **new_s);
274 PEMChain*       decodepemchain(char *s, char *type);
275 uchar*          X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
276 uchar*          X509req(RSApriv *priv, char *subj, int *certlen);
277 char*           X509verifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, RSApub *pk);
278 char*           X509verifydata(uchar *sig, int siglen, uchar *data, int datalen, RSApub *pk);
279 char*           X509verify(uchar *cert, int ncert, RSApub *pk);
280 void            X509dump(uchar *cert, int ncert);
281
282 /*
283  * elgamal
284  */
285 typedef struct EGpub EGpub;
286 typedef struct EGpriv EGpriv;
287 typedef struct EGsig EGsig;
288
289 /* public/encryption key */
290 struct EGpub
291 {
292         mpint   *p;     /* modulus */
293         mpint   *alpha; /* generator */
294         mpint   *key;   /* (encryption key) alpha**secret mod p */
295 };
296
297 /* private/decryption key */
298 struct EGpriv
299 {
300         EGpub   pub;
301         mpint   *secret;        /* (decryption key) */
302 };
303
304 /* signature */
305 struct EGsig
306 {
307         mpint   *r, *s;
308 };
309
310 EGpriv*         eggen(int nlen, int rounds);
311 mpint*          egencrypt(EGpub *k, mpint *in, mpint *out);     /* deprecated */
312 mpint*          egdecrypt(EGpriv *k, mpint *in, mpint *out);
313 EGsig*          egsign(EGpriv *k, mpint *m);
314 int             egverify(EGpub *k, EGsig *sig, mpint *m);
315 EGpub*          egpuballoc(void);
316 void            egpubfree(EGpub*);
317 EGpriv*         egprivalloc(void);
318 void            egprivfree(EGpriv*);
319 EGsig*          egsigalloc(void);
320 void            egsigfree(EGsig*);
321 EGpub*          egprivtopub(EGpriv*);
322
323 /*
324  * dsa
325  */
326 typedef struct DSApub DSApub;
327 typedef struct DSApriv DSApriv;
328 typedef struct DSAsig DSAsig;
329
330 /* public/encryption key */
331 struct DSApub
332 {
333         mpint   *p;     /* modulus */
334         mpint   *q;     /* group order, q divides p-1 */
335         mpint   *alpha; /* group generator */
336         mpint   *key;   /* (encryption key) alpha**secret mod p */
337 };
338
339 /* private/decryption key */
340 struct DSApriv
341 {
342         DSApub  pub;
343         mpint   *secret;        /* (decryption key) */
344 };
345
346 /* signature */
347 struct DSAsig
348 {
349         mpint   *r, *s;
350 };
351
352 DSApriv*        dsagen(DSApub *opub);   /* opub not checked for consistency! */
353 DSAsig*         dsasign(DSApriv *k, mpint *m);
354 int             dsaverify(DSApub *k, DSAsig *sig, mpint *m);
355 DSApub*         dsapuballoc(void);
356 void            dsapubfree(DSApub*);
357 DSApriv*        dsaprivalloc(void);
358 void            dsaprivfree(DSApriv*);
359 DSAsig*         dsasigalloc(void);
360 void            dsasigfree(DSAsig*);
361 DSApub*         dsaprivtopub(DSApriv*);
362 DSApriv*        asn1toDSApriv(uchar*, int);
363
364 /*
365  * TLS
366  */
367 typedef struct Thumbprint{
368         struct Thumbprint *next;
369         uchar   sha1[SHA1dlen];
370 } Thumbprint;
371
372 typedef struct TLSconn{
373         char    dir[40];        /* connection directory */
374         uchar   *cert;  /* certificate (local on input, remote on output) */
375         uchar   *sessionID;
376         int     certlen;
377         int     sessionIDlen;
378         int     (*trace)(char*fmt, ...);
379         PEMChain*chain; /* optional extra certificate evidence for servers to present */
380         char    *sessionType;
381         uchar   *sessionKey;
382         int     sessionKeylen;
383         char    *sessionConst;
384         char    *serverName;
385 } TLSconn;
386
387 /* tlshand.c */
388 int tlsClient(int fd, TLSconn *c);
389 int tlsServer(int fd, TLSconn *c);
390
391 /* thumb.c */
392 Thumbprint* initThumbprints(char *ok, char *crl);
393 void    freeThumbprints(Thumbprint *ok);
394 int     okThumbprint(uchar *sha1, Thumbprint *ok);
395
396 /* readcert.c */
397 uchar   *readcert(char *filename, int *pcertlen);
398 PEMChain*readcertchain(char *filename);
399
400 /* aes_xts.c */
401 int aes_xts_encrypt(ulong tweak[], ulong ecb[],  vlong sectorNumber, uchar *input, uchar *output, ulong len) ;
402 int aes_xts_decrypt(ulong tweak[], ulong ecb[], vlong sectorNumber, uchar *input, uchar *output, ulong len);
403
404 typedef struct ECpoint{
405         int inf;
406         mpint *x;
407         mpint *y;
408 } ECpoint;
409
410 typedef ECpoint ECpub;
411 typedef struct ECpriv{
412         ECpoint;
413         mpint *d;
414 } ECpriv;
415
416 typedef struct ECdomain{
417         mpint *p;
418         mpint *a;
419         mpint *b;
420         ECpoint *G;
421         mpint *n;
422         mpint *h;
423 } ECdomain;
424
425 void    ecassign(ECdomain *, ECpoint *old, ECpoint *new);
426 void    ecadd(ECdomain *, ECpoint *a, ECpoint *b, ECpoint *s);
427 void    ecmul(ECdomain *, ECpoint *a, mpint *k, ECpoint *s);
428 ECpoint*        strtoec(ECdomain *, char *, char **, ECpoint *);
429 ECpriv* ecgen(ECdomain *, ECpriv*);
430 int     ecverify(ECdomain *, ECpoint *);
431 int     ecpubverify(ECdomain *, ECpub *);
432 void    ecdsasign(ECdomain *, ECpriv *, uchar *, int, mpint *, mpint *);
433 int     ecdsaverify(ECdomain *, ECpub *, uchar *, int, mpint *, mpint *);
434 void    base58enc(uchar *, char *, int);
435 int     base58dec(char *, uchar *, int);
436
437 DigestState*    ripemd160(uchar *, ulong, uchar *, DigestState *);
438
439 /*
440  * Diffie-Hellman key exchange
441  */
442
443 typedef struct DHstate DHstate;
444 struct DHstate
445 {
446         mpint   *g;     /* base g */
447         mpint   *p;     /* large prime */
448         mpint   *q;     /* subgroup prime */
449         mpint   *x;     /* random secret */
450         mpint   *y;     /* public key y = g**x % p */
451 };
452
453 /* generate new public key: y = g**x % p */
454 mpint* dh_new(DHstate *dh, mpint *p, mpint *q, mpint *g);
455
456 /* calculate shared key: k = y**x % p */
457 mpint* dh_finish(DHstate *dh, mpint *y);
458
459 /* Curve25519 elliptic curve, public key function */
460 void curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]);
461
462 /* Curve25519 diffie hellman */
463 void curve25519_dh_new(uchar x[32], uchar y[32]);
464 void curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]);
465
466 /* password-based key derivation function 2 (rfc2898) */
467 void pbkdf2_x(uchar *p, ulong plen, uchar *s, ulong slen, ulong rounds, uchar *d, ulong dlen,
468         DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
469
470 /* hmac-based key derivation function (rfc5869) */
471 void hkdf_x(uchar *salt, ulong nsalt, uchar *info, ulong ninfo, uchar *key, ulong nkey, uchar *d, ulong dlen,
472         DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
473