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