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