]> git.lizzy.rs Git - plan9front.git/blob - sys/include/libsec.h
99df9791b537520cc8738e0c083520e1a3a0d570
[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 typedef struct AESGCMstate AESGCMstate;
45 struct AESGCMstate
46 {
47         AESstate;
48
49         ulong   H[4];
50         ulong   M[16][256][4];
51 };
52
53 void    setupAESGCMstate(AESGCMstate *s, uchar *key, int keylen, uchar *iv, int ivlen);
54 void    aesgcm_setiv(AESGCMstate *s, uchar *iv, int ivlen);
55 void    aesgcm_encrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], AESGCMstate *s);
56 int     aesgcm_decrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], AESGCMstate *s);
57
58 /*
59  * Blowfish Definitions
60  */
61
62 enum
63 {
64         BFbsize = 8,
65         BFrounds= 16
66 };
67
68 /* 16-round Blowfish */
69 typedef struct BFstate BFstate;
70 struct BFstate
71 {
72         ulong   setup;
73
74         uchar   key[56];
75         uchar   ivec[8];
76
77         u32int  pbox[BFrounds+2];
78         u32int  sbox[1024];
79 };
80
81 void    setupBFstate(BFstate *s, uchar key[], int keybytes, uchar *ivec);
82 void    bfCBCencrypt(uchar*, int, BFstate*);
83 void    bfCBCdecrypt(uchar*, int, BFstate*);
84 void    bfECBencrypt(uchar*, int, BFstate*);
85 void    bfECBdecrypt(uchar*, int, BFstate*);
86
87 /*
88  * Chacha definitions
89  */
90
91 enum
92 {
93         ChachaBsize=    64,
94         ChachaKeylen=   256/8,
95         ChachaIVlen=    96/8,
96         XChachaIVlen=   192/8,
97 };
98
99 typedef struct Chachastate Chachastate;
100 struct Chachastate
101 {
102         union{
103                 u32int  input[16];
104                 struct {
105                         u32int  constant[4];
106                         u32int  key[8];
107                         u32int  counter;
108                         u32int  iv[3];
109                 };
110         };
111         u32int  xkey[8];
112         int     rounds;
113         int     ivwords;
114 };
115
116 void    setupChachastate(Chachastate*, uchar*, ulong, uchar*, ulong, int);
117 void    chacha_setiv(Chachastate *, uchar*);
118 void    chacha_setblock(Chachastate*, u64int);
119 void    chacha_encrypt(uchar*, ulong, Chachastate*);
120 void    chacha_encrypt2(uchar*, uchar*, ulong, Chachastate*);
121
122 void    hchacha(uchar h[32], uchar *key, ulong keylen, uchar nonce[16], int rounds);
123
124 void    ccpoly_encrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], Chachastate *cs);
125 int     ccpoly_decrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], Chachastate *cs);
126
127 /*
128  * Salsa definitions
129  */
130 enum
131 {
132         SalsaBsize=     64,
133         SalsaKeylen=    256/8,
134         SalsaIVlen=     64/8,
135         XSalsaIVlen=    192/8,
136 };
137
138 typedef struct Salsastate Salsastate;
139 struct Salsastate
140 {
141         u32int  input[16];
142         u32int  xkey[8];
143         int     rounds;
144         int     ivwords;
145 };
146
147 void    setupSalsastate(Salsastate*, uchar*, ulong, uchar*, ulong, int);
148 void    salsa_setiv(Salsastate*, uchar*);
149 void    salsa_setblock(Salsastate*, u64int);
150 void    salsa_encrypt(uchar*, ulong, Salsastate*);
151 void    salsa_encrypt2(uchar*, uchar*, ulong, Salsastate*);
152
153 void    salsa_core(u32int in[16], u32int out[16], int rounds);
154
155 void    hsalsa(uchar h[32], uchar *key, ulong keylen, uchar nonce[16], int rounds);
156
157 /*
158  * DES definitions
159  */
160
161 enum
162 {
163         DESbsize=       8
164 };
165
166 /* single des */
167 typedef struct DESstate DESstate;
168 struct DESstate
169 {
170         ulong   setup;
171         uchar   key[8];         /* unexpanded key */
172         ulong   expanded[32];   /* expanded key */
173         uchar   ivec[8];        /* initialization vector */
174 };
175
176 void    setupDESstate(DESstate *s, uchar key[8], uchar *ivec);
177 void    des_key_setup(uchar[8], ulong[32]);
178 void    block_cipher(ulong*, uchar*, int);
179 void    desCBCencrypt(uchar*, int, DESstate*);
180 void    desCBCdecrypt(uchar*, int, DESstate*);
181 void    desECBencrypt(uchar*, int, DESstate*);
182 void    desECBdecrypt(uchar*, int, DESstate*);
183
184 /* for backward compatibility with 7-byte DES key format */
185 void    des56to64(uchar *k56, uchar *k64);
186 void    des64to56(uchar *k64, uchar *k56);
187 void    key_setup(uchar[7], ulong[32]);
188
189 /* triple des encrypt/decrypt orderings */
190 enum {
191         DES3E=          0,
192         DES3D=          1,
193         DES3EEE=        0,
194         DES3EDE=        2,
195         DES3DED=        5,
196         DES3DDD=        7
197 };
198
199 typedef struct DES3state DES3state;
200 struct DES3state
201 {
202         ulong   setup;
203         uchar   key[3][8];              /* unexpanded key */
204         ulong   expanded[3][32];        /* expanded key */
205         uchar   ivec[8];                /* initialization vector */
206 };
207
208 void    setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec);
209 void    triple_block_cipher(ulong keys[3][32], uchar*, int);
210 void    des3CBCencrypt(uchar*, int, DES3state*);
211 void    des3CBCdecrypt(uchar*, int, DES3state*);
212 void    des3ECBencrypt(uchar*, int, DES3state*);
213 void    des3ECBdecrypt(uchar*, int, DES3state*);
214
215 /*
216  * digests
217  */
218
219 enum
220 {
221         SHA1dlen=       20,     /* SHA digest length */
222         SHA2_224dlen=   28,     /* SHA-224 digest length */
223         SHA2_256dlen=   32,     /* SHA-256 digest length */
224         SHA2_384dlen=   48,     /* SHA-384 digest length */
225         SHA2_512dlen=   64,     /* SHA-512 digest length */
226         MD4dlen=        16,     /* MD4 digest length */
227         MD5dlen=        16,     /* MD5 digest length */
228         Poly1305dlen=   16,     /* Poly1305 digest length */
229
230         Hmacblksz       = 64,   /* in bytes; from rfc2104 */
231 };
232
233 typedef struct DigestState DigestState;
234 struct DigestState
235 {
236         uvlong  len;
237         union {
238                 u32int  state[16];
239                 u64int  bstate[8];
240         };
241         uchar   buf[256];
242         int     blen;
243         char    malloced;
244         char    seeded;
245 };
246 typedef struct DigestState SHAstate;    /* obsolete name */
247 typedef struct DigestState SHA1state;
248 typedef struct DigestState SHA2_224state;
249 typedef struct DigestState SHA2_256state;
250 typedef struct DigestState SHA2_384state;
251 typedef struct DigestState SHA2_512state;
252 typedef struct DigestState MD5state;
253 typedef struct DigestState MD4state;
254
255 DigestState*    md4(uchar*, ulong, uchar*, DigestState*);
256 DigestState*    md5(uchar*, ulong, uchar*, DigestState*);
257 DigestState*    sha1(uchar*, ulong, uchar*, DigestState*);
258 DigestState*    sha2_224(uchar*, ulong, uchar*, DigestState*);
259 DigestState*    sha2_256(uchar*, ulong, uchar*, DigestState*);
260 DigestState*    sha2_384(uchar*, ulong, uchar*, DigestState*);
261 DigestState*    sha2_512(uchar*, ulong, uchar*, DigestState*);
262 DigestState*    hmac_x(uchar *p, ulong len, uchar *key, ulong klen,
263                         uchar *digest, DigestState *s,
264                         DigestState*(*x)(uchar*, ulong, uchar*, DigestState*),
265                         int xlen);
266 DigestState*    hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
267 DigestState*    hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
268 DigestState*    hmac_sha2_224(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
269 DigestState*    hmac_sha2_256(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
270 DigestState*    hmac_sha2_384(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
271 DigestState*    hmac_sha2_512(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
272 char*           md5pickle(MD5state*);
273 MD5state*       md5unpickle(char*);
274 char*           sha1pickle(SHA1state*);
275 SHA1state*      sha1unpickle(char*);
276
277 DigestState*    poly1305(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
278
279 /*
280  * random number generation
281  */
282 void    genrandom(uchar *buf, int nbytes);
283 void    prng(uchar *buf, int nbytes);
284 ulong   fastrand(void);
285 ulong   nfastrand(ulong);
286
287 /*
288  * primes
289  */
290 void    genprime(mpint *p, int n, int accuracy); /* generate n-bit probable prime */
291 void    gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); /* prime & generator */
292 void    genstrongprime(mpint *p, int n, int accuracy); /* generate n-bit strong prime */
293 void    DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
294 int     probably_prime(mpint *n, int nrep);     /* miller-rabin test */
295 int     smallprimetest(mpint *p);  /* returns -1 if not prime, 0 otherwise */
296
297 /*
298  * rc4
299  */
300 typedef struct RC4state RC4state;
301 struct RC4state
302 {
303          uchar  state[256];
304          uchar  x;
305          uchar  y;
306 };
307
308 void    setupRC4state(RC4state*, uchar*, int);
309 void    rc4(RC4state*, uchar*, int);
310 void    rc4skip(RC4state*, int);
311 void    rc4back(RC4state*, int);
312
313 /*
314  * rsa
315  */
316 typedef struct RSApub RSApub;
317 typedef struct RSApriv RSApriv;
318 typedef struct PEMChain PEMChain;
319
320 /* public/encryption key */
321 struct RSApub
322 {
323         mpint   *n;     /* modulus */
324         mpint   *ek;    /* exp (encryption key) */
325 };
326
327 /* private/decryption key */
328 struct RSApriv
329 {
330         RSApub  pub;
331
332         mpint   *dk;    /* exp (decryption key) */
333
334         /* precomputed values to help with chinese remainder theorem calc */
335         mpint   *p;
336         mpint   *q;
337         mpint   *kp;    /* dk mod p-1 */
338         mpint   *kq;    /* dk mod q-1 */
339         mpint   *c2;    /* (inv p) mod q */
340 };
341
342 struct PEMChain{
343         PEMChain*next;
344         uchar   *pem;
345         int     pemlen;
346 };
347
348 RSApriv*        rsagen(int nlen, int elen, int rounds);
349 RSApriv*        rsafill(mpint *n, mpint *e, mpint *d, mpint *p, mpint *q);
350 mpint*          rsaencrypt(RSApub *k, mpint *in, mpint *out);
351 mpint*          rsadecrypt(RSApriv *k, mpint *in, mpint *out);
352 RSApub*         rsapuballoc(void);
353 void            rsapubfree(RSApub*);
354 RSApriv*        rsaprivalloc(void);
355 void            rsaprivfree(RSApriv*);
356 RSApub*         rsaprivtopub(RSApriv*);
357 RSApub*         X509toRSApub(uchar*, int, char*, int);
358 RSApub*         asn1toRSApub(uchar*, int);
359 RSApriv*        asn1toRSApriv(uchar*, int);
360 void            asn1dump(uchar *der, int len);
361 uchar*          decodePEM(char *s, char *type, int *len, char **new_s);
362 PEMChain*       decodepemchain(char *s, char *type);
363 uchar*          X509rsagen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
364 uchar*          X509rsareq(RSApriv *priv, char *subj, int *certlen);
365 char*           X509rsaverify(uchar *cert, int ncert, RSApub *pk);
366 char*           X509rsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, RSApub *pk);
367
368 void            X509dump(uchar *cert, int ncert);
369
370 mpint*          pkcs1padbuf(uchar *buf, int len, mpint *modulus, int blocktype);
371 int             pkcs1unpadbuf(uchar *buf, int len, mpint *modulus, int blocktype);
372 int             asn1encodeRSApub(RSApub *pk, uchar *buf, int len);
373 int             asn1encodedigest(DigestState* (*fun)(uchar*, ulong, uchar*, DigestState*),
374                         uchar *digest, uchar *buf, int len);
375
376
377 /*
378  * elgamal
379  */
380 typedef struct EGpub EGpub;
381 typedef struct EGpriv EGpriv;
382 typedef struct EGsig EGsig;
383
384 /* public/encryption key */
385 struct EGpub
386 {
387         mpint   *p;     /* modulus */
388         mpint   *alpha; /* generator */
389         mpint   *key;   /* (encryption key) alpha**secret mod p */
390 };
391
392 /* private/decryption key */
393 struct EGpriv
394 {
395         EGpub   pub;
396         mpint   *secret;        /* (decryption key) */
397 };
398
399 /* signature */
400 struct EGsig
401 {
402         mpint   *r, *s;
403 };
404
405 EGpriv*         eggen(int nlen, int rounds);
406 mpint*          egencrypt(EGpub *k, mpint *in, mpint *out);     /* deprecated */
407 mpint*          egdecrypt(EGpriv *k, mpint *in, mpint *out);
408 EGsig*          egsign(EGpriv *k, mpint *m);
409 int             egverify(EGpub *k, EGsig *sig, mpint *m);
410 EGpub*          egpuballoc(void);
411 void            egpubfree(EGpub*);
412 EGpriv*         egprivalloc(void);
413 void            egprivfree(EGpriv*);
414 EGsig*          egsigalloc(void);
415 void            egsigfree(EGsig*);
416 EGpub*          egprivtopub(EGpriv*);
417
418 /*
419  * dsa
420  */
421 typedef struct DSApub DSApub;
422 typedef struct DSApriv DSApriv;
423 typedef struct DSAsig DSAsig;
424
425 /* public/encryption key */
426 struct DSApub
427 {
428         mpint   *p;     /* modulus */
429         mpint   *q;     /* group order, q divides p-1 */
430         mpint   *alpha; /* group generator */
431         mpint   *key;   /* (encryption key) alpha**secret mod p */
432 };
433
434 /* private/decryption key */
435 struct DSApriv
436 {
437         DSApub  pub;
438         mpint   *secret;        /* (decryption key) */
439 };
440
441 /* signature */
442 struct DSAsig
443 {
444         mpint   *r, *s;
445 };
446
447 DSApriv*        dsagen(DSApub *opub);   /* opub not checked for consistency! */
448 DSAsig*         dsasign(DSApriv *k, mpint *m);
449 int             dsaverify(DSApub *k, DSAsig *sig, mpint *m);
450 DSApub*         dsapuballoc(void);
451 void            dsapubfree(DSApub*);
452 DSApriv*        dsaprivalloc(void);
453 void            dsaprivfree(DSApriv*);
454 DSAsig*         dsasigalloc(void);
455 void            dsasigfree(DSAsig*);
456 DSApub*         dsaprivtopub(DSApriv*);
457 DSApriv*        asn1toDSApriv(uchar*, int);
458
459 /*
460  * TLS
461  */
462 typedef struct Thumbprint{
463         struct Thumbprint *next;
464         uchar   hash[SHA2_256dlen];
465         uchar   len;
466 } Thumbprint;
467
468 typedef struct TLSconn{
469         char    dir[40];        /* connection directory */
470         uchar   *cert;  /* certificate (local on input, remote on output) */
471         uchar   *sessionID;
472         uchar   *psk;
473         int     certlen;
474         int     sessionIDlen;
475         int     psklen;
476         int     (*trace)(char*fmt, ...);
477         PEMChain*chain; /* optional extra certificate evidence for servers to present */
478         char    *sessionType;
479         uchar   *sessionKey;
480         int     sessionKeylen;
481         char    *sessionConst;
482         char    *serverName;
483         char    *pskID;
484 } TLSconn;
485
486 /* tlshand.c */
487 int tlsClient(int fd, TLSconn *c);
488 int tlsServer(int fd, TLSconn *c);
489
490 /* thumb.c */
491 Thumbprint* initThumbprints(char *ok, char *crl, char *tag);
492 void    freeThumbprints(Thumbprint *ok);
493 int     okThumbprint(uchar *hash, int len, Thumbprint *ok);
494 int     okCertificate(uchar *cert, int len, Thumbprint *ok);
495
496 /* readcert.c */
497 uchar   *readcert(char *filename, int *pcertlen);
498 PEMChain*readcertchain(char *filename);
499
500 /* aes_xts.c */
501 int aes_xts_encrypt(ulong tweak[], ulong ecb[],  vlong sectorNumber, uchar *input, uchar *output, ulong len) ;
502 int aes_xts_decrypt(ulong tweak[], ulong ecb[], vlong sectorNumber, uchar *input, uchar *output, ulong len);
503
504 typedef struct ECpoint{
505         int inf;
506         mpint *x;
507         mpint *y;
508         mpint *z;       /* nil when using affine coordinates */
509 } ECpoint;
510
511 typedef ECpoint ECpub;
512 typedef struct ECpriv{
513         ECpoint;
514         mpint *d;
515 } ECpriv;
516
517 typedef struct ECdomain{
518         mpint *p;
519         mpint *a;
520         mpint *b;
521         ECpoint G;
522         mpint *n;
523         mpint *h;
524 } ECdomain;
525
526 void    ecdominit(ECdomain *, void (*init)(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h));
527 void    ecdomfree(ECdomain *);
528
529 void    ecassign(ECdomain *, ECpoint *old, ECpoint *new);
530 void    ecadd(ECdomain *, ECpoint *a, ECpoint *b, ECpoint *s);
531 void    ecmul(ECdomain *, ECpoint *a, mpint *k, ECpoint *s);
532 ECpoint*        strtoec(ECdomain *, char *, char **, ECpoint *);
533 ECpriv* ecgen(ECdomain *, ECpriv*);
534 int     ecverify(ECdomain *, ECpoint *);
535 int     ecpubverify(ECdomain *, ECpub *);
536 void    ecdsasign(ECdomain *, ECpriv *, uchar *, int, mpint *, mpint *);
537 int     ecdsaverify(ECdomain *, ECpub *, uchar *, int, mpint *, mpint *);
538 void    base58enc(uchar *, char *, int);
539 int     base58dec(char *, uchar *, int);
540
541 ECpub*  ecdecodepub(ECdomain *dom, uchar *, int);
542 int     ecencodepub(ECdomain *dom, ECpub *, uchar *, int);
543 void    ecpubfree(ECpub *);
544
545 ECpub*  X509toECpub(uchar *cert, int ncert, char *name, int nname, ECdomain *dom);
546 char*   X509ecdsaverify(uchar *cert, int ncert, ECdomain *dom, ECpub *pub);
547 char*   X509ecdsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, ECdomain *dom, ECpub *pub);
548
549 /* curves */
550 void    secp256r1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h);
551 void    secp256k1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h);
552 void    secp384r1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h);
553
554 DigestState*    ripemd160(uchar *, ulong, uchar *, DigestState *);
555
556 /*
557  * Diffie-Hellman key exchange
558  */
559
560 typedef struct DHstate DHstate;
561 struct DHstate
562 {
563         mpint   *g;     /* base g */
564         mpint   *p;     /* large prime */
565         mpint   *q;     /* subgroup prime */
566         mpint   *x;     /* random secret */
567         mpint   *y;     /* public key y = g**x % p */
568 };
569
570 /* generate new public key: y = g**x % p */
571 mpint* dh_new(DHstate *dh, mpint *p, mpint *q, mpint *g);
572
573 /* calculate shared key: k = y**x % p */
574 mpint* dh_finish(DHstate *dh, mpint *y);
575
576 /* Curve25519 elliptic curve, public key function */
577 void curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]);
578
579 /* Curve25519 diffie hellman */
580 void curve25519_dh_new(uchar x[32], uchar y[32]);
581 void curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]);
582
583 /* password-based key derivation function 2 (rfc2898) */
584 void pbkdf2_x(uchar *p, ulong plen, uchar *s, ulong slen, ulong rounds, uchar *d, ulong dlen,
585         DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
586
587 /* scrypt password-based key derivation function */
588 char* scrypt(uchar *p, ulong plen, uchar *s, ulong slen,
589         ulong N, ulong R, ulong P,
590         uchar *d, ulong dlen);
591
592 /* hmac-based key derivation function (rfc5869) */
593 void hkdf_x(uchar *salt, ulong nsalt, uchar *info, ulong ninfo, uchar *key, ulong nkey, uchar *d, ulong dlen,
594         DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
595
596 /* timing safe memcmp() */
597 int tsmemcmp(void*, void*, ulong);