]> git.lizzy.rs Git - plan9front.git/blob - sys/include/libsec.h
d8d4852d5cbf0d0a43b838f750f8c5c594fcc8fb
[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         AESdlen=        16,     /* TODO: see rfc */
145
146         Hmacblksz       = 64,   /* in bytes; from rfc2104 */
147 };
148
149 typedef struct DigestState DigestState;
150 struct DigestState
151 {
152         uvlong  len;
153         union {
154                 u32int  state[8];
155                 u64int  bstate[8];
156         };
157         uchar   buf[256];
158         int     blen;
159         char    malloced;
160         char    seeded;
161 };
162 typedef struct DigestState SHAstate;    /* obsolete name */
163 typedef struct DigestState SHA1state;
164 typedef struct DigestState SHA2_224state;
165 typedef struct DigestState SHA2_256state;
166 typedef struct DigestState SHA2_384state;
167 typedef struct DigestState SHA2_512state;
168 typedef struct DigestState MD5state;
169 typedef struct DigestState MD4state;
170 typedef struct DigestState AEShstate;
171
172 DigestState*    md4(uchar*, ulong, uchar*, DigestState*);
173 DigestState*    md5(uchar*, ulong, uchar*, DigestState*);
174 DigestState*    sha1(uchar*, ulong, uchar*, DigestState*);
175 DigestState*    sha2_224(uchar*, ulong, uchar*, DigestState*);
176 DigestState*    sha2_256(uchar*, ulong, uchar*, DigestState*);
177 DigestState*    sha2_384(uchar*, ulong, uchar*, DigestState*);
178 DigestState*    sha2_512(uchar*, ulong, uchar*, DigestState*);
179 DigestState*    aes(uchar*, ulong, uchar*, DigestState*);
180 DigestState*    hmac_x(uchar *p, ulong len, uchar *key, ulong klen,
181                         uchar *digest, DigestState *s,
182                         DigestState*(*x)(uchar*, ulong, uchar*, DigestState*),
183                         int xlen);
184 DigestState*    hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
185 DigestState*    hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
186 DigestState*    hmac_sha2_224(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
187 DigestState*    hmac_sha2_256(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
188 DigestState*    hmac_sha2_384(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
189 DigestState*    hmac_sha2_512(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
190 DigestState*    hmac_aes(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
191 char*           md5pickle(MD5state*);
192 MD5state*       md5unpickle(char*);
193 char*           sha1pickle(SHA1state*);
194 SHA1state*      sha1unpickle(char*);
195
196 /*
197  * random number generation
198  */
199 void    genrandom(uchar *buf, int nbytes);
200 void    prng(uchar *buf, int nbytes);
201 ulong   fastrand(void);
202 ulong   nfastrand(ulong);
203
204 /*
205  * primes
206  */
207 void    genprime(mpint *p, int n, int accuracy); /* generate n-bit probable prime */
208 void    gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); /* prime & generator */
209 void    genstrongprime(mpint *p, int n, int accuracy); /* generate n-bit strong prime */
210 void    DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
211 int     probably_prime(mpint *n, int nrep);     /* miller-rabin test */
212 int     smallprimetest(mpint *p);  /* returns -1 if not prime, 0 otherwise */
213
214 /*
215  * rc4
216  */
217 typedef struct RC4state RC4state;
218 struct RC4state
219 {
220          uchar  state[256];
221          uchar  x;
222          uchar  y;
223 };
224
225 void    setupRC4state(RC4state*, uchar*, int);
226 void    rc4(RC4state*, uchar*, int);
227 void    rc4skip(RC4state*, int);
228 void    rc4back(RC4state*, int);
229
230 /*
231  * rsa
232  */
233 typedef struct RSApub RSApub;
234 typedef struct RSApriv RSApriv;
235 typedef struct PEMChain PEMChain;
236
237 /* public/encryption key */
238 struct RSApub
239 {
240         mpint   *n;     /* modulus */
241         mpint   *ek;    /* exp (encryption key) */
242 };
243
244 /* private/decryption key */
245 struct RSApriv
246 {
247         RSApub  pub;
248
249         mpint   *dk;    /* exp (decryption key) */
250
251         /* precomputed values to help with chinese remainder theorem calc */
252         mpint   *p;
253         mpint   *q;
254         mpint   *kp;    /* dk mod p-1 */
255         mpint   *kq;    /* dk mod q-1 */
256         mpint   *c2;    /* (inv p) mod q */
257 };
258
259 struct PEMChain{
260         PEMChain*next;
261         uchar   *pem;
262         int     pemlen;
263 };
264
265 RSApriv*        rsagen(int nlen, int elen, int rounds);
266 RSApriv*        rsafill(mpint *n, mpint *e, mpint *d, mpint *p, mpint *q);
267 mpint*          rsaencrypt(RSApub *k, mpint *in, mpint *out);
268 mpint*          rsadecrypt(RSApriv *k, mpint *in, mpint *out);
269 RSApub*         rsapuballoc(void);
270 void            rsapubfree(RSApub*);
271 RSApriv*        rsaprivalloc(void);
272 void            rsaprivfree(RSApriv*);
273 RSApub*         rsaprivtopub(RSApriv*);
274 RSApub*         X509toRSApub(uchar*, int, char*, int);
275 RSApriv*        asn1toRSApriv(uchar*, int);
276 void            asn1dump(uchar *der, int len);
277 uchar*          decodePEM(char *s, char *type, int *len, char **new_s);
278 PEMChain*       decodepemchain(char *s, char *type);
279 uchar*          X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
280 uchar*          X509req(RSApriv *priv, char *subj, int *certlen);
281 char*           X509verify(uchar *cert, int ncert, RSApub *pk);
282 void            X509dump(uchar *cert, int ncert);
283
284 /*
285  * elgamal
286  */
287 typedef struct EGpub EGpub;
288 typedef struct EGpriv EGpriv;
289 typedef struct EGsig EGsig;
290
291 /* public/encryption key */
292 struct EGpub
293 {
294         mpint   *p;     /* modulus */
295         mpint   *alpha; /* generator */
296         mpint   *key;   /* (encryption key) alpha**secret mod p */
297 };
298
299 /* private/decryption key */
300 struct EGpriv
301 {
302         EGpub   pub;
303         mpint   *secret;        /* (decryption key) */
304 };
305
306 /* signature */
307 struct EGsig
308 {
309         mpint   *r, *s;
310 };
311
312 EGpriv*         eggen(int nlen, int rounds);
313 mpint*          egencrypt(EGpub *k, mpint *in, mpint *out);     /* deprecated */
314 mpint*          egdecrypt(EGpriv *k, mpint *in, mpint *out);
315 EGsig*          egsign(EGpriv *k, mpint *m);
316 int             egverify(EGpub *k, EGsig *sig, mpint *m);
317 EGpub*          egpuballoc(void);
318 void            egpubfree(EGpub*);
319 EGpriv*         egprivalloc(void);
320 void            egprivfree(EGpriv*);
321 EGsig*          egsigalloc(void);
322 void            egsigfree(EGsig*);
323 EGpub*          egprivtopub(EGpriv*);
324
325 /*
326  * dsa
327  */
328 typedef struct DSApub DSApub;
329 typedef struct DSApriv DSApriv;
330 typedef struct DSAsig DSAsig;
331
332 /* public/encryption key */
333 struct DSApub
334 {
335         mpint   *p;     /* modulus */
336         mpint   *q;     /* group order, q divides p-1 */
337         mpint   *alpha; /* group generator */
338         mpint   *key;   /* (encryption key) alpha**secret mod p */
339 };
340
341 /* private/decryption key */
342 struct DSApriv
343 {
344         DSApub  pub;
345         mpint   *secret;        /* (decryption key) */
346 };
347
348 /* signature */
349 struct DSAsig
350 {
351         mpint   *r, *s;
352 };
353
354 DSApriv*        dsagen(DSApub *opub);   /* opub not checked for consistency! */
355 DSAsig*         dsasign(DSApriv *k, mpint *m);
356 int             dsaverify(DSApub *k, DSAsig *sig, mpint *m);
357 DSApub*         dsapuballoc(void);
358 void            dsapubfree(DSApub*);
359 DSApriv*        dsaprivalloc(void);
360 void            dsaprivfree(DSApriv*);
361 DSAsig*         dsasigalloc(void);
362 void            dsasigfree(DSAsig*);
363 DSApub*         dsaprivtopub(DSApriv*);
364 DSApriv*        asn1toDSApriv(uchar*, int);
365
366 /*
367  * TLS
368  */
369 typedef struct Thumbprint{
370         struct Thumbprint *next;
371         uchar   sha1[SHA1dlen];
372 } Thumbprint;
373
374 typedef struct TLSconn{
375         char    dir[40];        /* connection directory */
376         uchar   *cert;  /* certificate (local on input, remote on output) */
377         uchar   *sessionID;
378         int     certlen;
379         int     sessionIDlen;
380         int     (*trace)(char*fmt, ...);
381         PEMChain*chain; /* optional extra certificate evidence for servers to present */
382         char    *sessionType;
383         uchar   *sessionKey;
384         int     sessionKeylen;
385         char    *sessionConst;
386         char    *serverName;
387 } TLSconn;
388
389 /* tlshand.c */
390 int tlsClient(int fd, TLSconn *c);
391 int tlsServer(int fd, TLSconn *c);
392
393 /* thumb.c */
394 Thumbprint* initThumbprints(char *ok, char *crl);
395 void    freeThumbprints(Thumbprint *ok);
396 int     okThumbprint(uchar *sha1, Thumbprint *ok);
397
398 /* readcert.c */
399 uchar   *readcert(char *filename, int *pcertlen);
400 PEMChain*readcertchain(char *filename);
401
402 /* aes_xts.c */
403 int aes_xts_encrypt(ulong tweak[], ulong ecb[],  vlong sectorNumber, uchar *input, uchar *output, ulong len) ;
404 int aes_xts_decrypt(ulong tweak[], ulong ecb[], vlong sectorNumber, uchar *input, uchar *output, ulong len);
405
406 /*
407  * ECC
408  */
409
410 /* ids for ecnamedcurve */
411 enum
412 {
413         Secp256r1       = 23,
414 };
415
416 typedef struct ECpoint{
417         int inf;
418         mpint *x;
419         mpint *y;
420 } ECpoint;
421
422 typedef ECpoint ECpub;
423 typedef struct ECpriv{
424         ECpoint;
425         mpint *d;
426 } ECpriv;
427
428 typedef struct ECdomain{
429         mpint *p;
430         mpint *a;
431         mpint *b;
432         ECpoint *G;
433         mpint *n;
434         mpint *h;
435 } ECdomain;
436
437 ECdomain*       ecnamedcurve(int);
438 void    ecfreepoint(ECpoint*);
439 void    ecfreepriv(ECpriv*);
440 void    ecfreedomain(ECdomain*);
441 void    ecassign(ECdomain *, ECpoint *old, ECpoint *new);
442 void    ecadd(ECdomain *, ECpoint *a, ECpoint *b, ECpoint *s);
443 void    ecmul(ECdomain *, ECpoint *a, mpint *k, ECpoint *s);
444 ECpoint*        betoec(ECdomain*, uchar*, int, ECpoint*);
445 ECpoint*        strtoec(ECdomain *, char *, char **, ECpoint*);
446 ECpriv* ecgen(ECdomain *, ECpriv*);
447 int     ecverify(ECdomain *, ECpoint *);
448 int     ecpubverify(ECdomain *, ECpub *);
449 void    ecdsasign(ECdomain *, ECpriv *, uchar *, int, mpint *, mpint *);
450 int     ecdsaverify(ECdomain *, ECpub *, uchar *, int, mpint *, mpint *);
451 void    base58enc(uchar *, char *, int);
452 int     base58dec(char *, uchar *, int);
453
454 DigestState*    ripemd160(uchar *, ulong, uchar *, DigestState *);
455
456 /*
457  * Diffie-Hellman key exchange
458  */
459
460 typedef struct DHstate DHstate;
461 struct DHstate
462 {
463         mpint   *g;     /* base g */
464         mpint   *p;     /* large prime */
465         mpint   *x;     /* random secret */
466         mpint   *y;     /* public key y = g ^ x % p */
467 };
468
469 /* generate new public key: y = g ^ x % p */
470 mpint* dh_new(DHstate *dh, mpint *p, mpint *g);
471
472 /* calculate shared key: k = pub ^ x % p */
473 mpint* dh_finish(DHstate *dh, mpint *pub);
474
475 /* constant-time comparison similar to memcmp(2) */
476 int constcmp(uchar *x, uchar *y, int len);
477
478 /* password-based key derivation function 2 (RFC 2898) */
479 void pbkdf2_hmac_sha1(uchar *p, ulong plen, uchar *s, ulong slen, ulong rounds, uchar *d, ulong dlen);