2 #pragma src "/sys/src/libsec"
6 typedef struct mpint mpint;
20 typedef struct AESstate AESstate;
27 uchar key[AESmaxkey]; /* unexpanded key */
28 ulong ekey[4*(AESmaxrounds + 1)]; /* encryption key */
29 ulong dkey[4*(AESmaxrounds + 1)]; /* decryption key */
30 uchar ivec[AESbsize]; /* initialization vector */
31 uchar mackey[3 * AESbsize]; /* 3 XCBC mac 96 keys */
35 void aes_encrypt(ulong rk[], int Nr, uchar pt[16], uchar ct[16]);
36 void aes_decrypt(ulong rk[], int Nr, uchar ct[16], uchar pt[16]);
38 void setupAESstate(AESstate *s, uchar key[], int keybytes, uchar *ivec);
39 void aesCBCencrypt(uchar *p, int len, AESstate *s);
40 void aesCBCdecrypt(uchar *p, int len, AESstate *s);
41 void aesCFBencrypt(uchar *p, int len, AESstate *s);
42 void aesCFBdecrypt(uchar *p, int len, AESstate *s);
43 void aesOFBencrypt(uchar *p, int len, AESstate *s);
45 void setupAESXCBCstate(AESstate *s);
46 uchar* aesXCBCmac(uchar *p, int len, AESstate *s);
48 typedef struct AESGCMstate AESGCMstate;
57 void setupAESGCMstate(AESGCMstate *s, uchar *key, int keylen, uchar *iv, int ivlen);
58 void aesgcm_setiv(AESGCMstate *s, uchar *iv, int ivlen);
59 void aesgcm_encrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], AESGCMstate *s);
60 int aesgcm_decrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], AESGCMstate *s);
63 * Blowfish Definitions
72 /* 16-round Blowfish */
73 typedef struct BFstate BFstate;
81 u32int pbox[BFrounds+2];
85 void setupBFstate(BFstate *s, uchar key[], int keybytes, uchar *ivec);
86 void bfCBCencrypt(uchar*, int, BFstate*);
87 void bfCBCdecrypt(uchar*, int, BFstate*);
88 void bfECBencrypt(uchar*, int, BFstate*);
89 void bfECBdecrypt(uchar*, int, BFstate*);
103 typedef struct Chachastate Chachastate;
120 void setupChachastate(Chachastate*, uchar*, ulong, uchar*, ulong, int);
121 void chacha_setiv(Chachastate *, uchar*);
122 void chacha_setblock(Chachastate*, u64int);
123 void chacha_encrypt(uchar*, ulong, Chachastate*);
124 void chacha_encrypt2(uchar*, uchar*, ulong, Chachastate*);
126 void hchacha(uchar h[32], uchar *key, ulong keylen, uchar nonce[16], int rounds);
128 void ccpoly_encrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], Chachastate *cs);
129 int ccpoly_decrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], Chachastate *cs);
142 typedef struct Salsastate Salsastate;
151 void setupSalsastate(Salsastate*, uchar*, ulong, uchar*, ulong, int);
152 void salsa_setiv(Salsastate*, uchar*);
153 void salsa_setblock(Salsastate*, u64int);
154 void salsa_encrypt(uchar*, ulong, Salsastate*);
155 void salsa_encrypt2(uchar*, uchar*, ulong, Salsastate*);
157 void salsa_core(u32int in[16], u32int out[16], int rounds);
159 void hsalsa(uchar h[32], uchar *key, ulong keylen, uchar nonce[16], int rounds);
171 typedef struct DESstate DESstate;
175 uchar key[8]; /* unexpanded key */
176 ulong expanded[32]; /* expanded key */
177 uchar ivec[8]; /* initialization vector */
180 void setupDESstate(DESstate *s, uchar key[8], uchar *ivec);
181 void des_key_setup(uchar[8], ulong[32]);
182 void block_cipher(ulong*, uchar*, int);
183 void desCBCencrypt(uchar*, int, DESstate*);
184 void desCBCdecrypt(uchar*, int, DESstate*);
185 void desECBencrypt(uchar*, int, DESstate*);
186 void desECBdecrypt(uchar*, int, DESstate*);
188 /* for backward compatibility with 7-byte DES key format */
189 void des56to64(uchar *k56, uchar *k64);
190 void des64to56(uchar *k64, uchar *k56);
191 void key_setup(uchar[7], ulong[32]);
193 /* triple des encrypt/decrypt orderings */
203 typedef struct DES3state DES3state;
207 uchar key[3][8]; /* unexpanded key */
208 ulong expanded[3][32]; /* expanded key */
209 uchar ivec[8]; /* initialization vector */
212 void setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec);
213 void triple_block_cipher(ulong keys[3][32], uchar*, int);
214 void des3CBCencrypt(uchar*, int, DES3state*);
215 void des3CBCdecrypt(uchar*, int, DES3state*);
216 void des3ECBencrypt(uchar*, int, DES3state*);
217 void des3ECBdecrypt(uchar*, int, DES3state*);
225 SHA1dlen= 20, /* SHA digest length */
226 SHA2_224dlen= 28, /* SHA-224 digest length */
227 SHA2_256dlen= 32, /* SHA-256 digest length */
228 SHA2_384dlen= 48, /* SHA-384 digest length */
229 SHA2_512dlen= 64, /* SHA-512 digest length */
230 MD4dlen= 16, /* MD4 digest length */
231 MD5dlen= 16, /* MD5 digest length */
232 Poly1305dlen= 16, /* Poly1305 digest length */
234 Hmacblksz = 64, /* in bytes; from rfc2104 */
237 typedef struct DigestState DigestState;
250 typedef struct DigestState SHAstate; /* obsolete name */
251 typedef struct DigestState SHA1state;
252 typedef struct DigestState SHA2_224state;
253 typedef struct DigestState SHA2_256state;
254 typedef struct DigestState SHA2_384state;
255 typedef struct DigestState SHA2_512state;
256 typedef struct DigestState MD5state;
257 typedef struct DigestState MD4state;
259 DigestState* md4(uchar*, ulong, uchar*, DigestState*);
260 DigestState* md5(uchar*, ulong, uchar*, DigestState*);
261 DigestState* sha1(uchar*, ulong, uchar*, DigestState*);
262 DigestState* sha2_224(uchar*, ulong, uchar*, DigestState*);
263 DigestState* sha2_256(uchar*, ulong, uchar*, DigestState*);
264 DigestState* sha2_384(uchar*, ulong, uchar*, DigestState*);
265 DigestState* sha2_512(uchar*, ulong, uchar*, DigestState*);
266 DigestState* hmac_x(uchar *p, ulong len, uchar *key, ulong klen,
267 uchar *digest, DigestState *s,
268 DigestState*(*x)(uchar*, ulong, uchar*, DigestState*),
270 DigestState* hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
271 DigestState* hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
272 DigestState* hmac_sha2_224(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
273 DigestState* hmac_sha2_256(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
274 DigestState* hmac_sha2_384(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
275 DigestState* hmac_sha2_512(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
276 char* md5pickle(MD5state*);
277 MD5state* md5unpickle(char*);
278 char* sha1pickle(SHA1state*);
279 SHA1state* sha1unpickle(char*);
281 DigestState* poly1305(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
284 * random number generation
286 void genrandom(uchar *buf, int nbytes);
287 void prng(uchar *buf, int nbytes);
288 ulong fastrand(void);
289 ulong nfastrand(ulong);
294 void genprime(mpint *p, int n, int accuracy); /* generate n-bit probable prime */
295 void gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); /* prime & generator */
296 void genstrongprime(mpint *p, int n, int accuracy); /* generate n-bit strong prime */
297 void DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
298 int probably_prime(mpint *n, int nrep); /* miller-rabin test */
299 int smallprimetest(mpint *p); /* returns -1 if not prime, 0 otherwise */
304 typedef struct RC4state RC4state;
312 void setupRC4state(RC4state*, uchar*, int);
313 void rc4(RC4state*, uchar*, int);
314 void rc4skip(RC4state*, int);
315 void rc4back(RC4state*, int);
320 typedef struct RSApub RSApub;
321 typedef struct RSApriv RSApriv;
322 typedef struct PEMChain PEMChain;
324 /* public/encryption key */
327 mpint *n; /* modulus */
328 mpint *ek; /* exp (encryption key) */
331 /* private/decryption key */
336 mpint *dk; /* exp (decryption key) */
338 /* precomputed values to help with chinese remainder theorem calc */
341 mpint *kp; /* dk mod p-1 */
342 mpint *kq; /* dk mod q-1 */
343 mpint *c2; /* (inv p) mod q */
352 RSApriv* rsagen(int nlen, int elen, int rounds);
353 RSApriv* rsafill(mpint *n, mpint *e, mpint *d, mpint *p, mpint *q);
354 mpint* rsaencrypt(RSApub *k, mpint *in, mpint *out);
355 mpint* rsadecrypt(RSApriv *k, mpint *in, mpint *out);
356 RSApub* rsapuballoc(void);
357 void rsapubfree(RSApub*);
358 RSApriv* rsaprivalloc(void);
359 void rsaprivfree(RSApriv*);
360 RSApub* rsaprivtopub(RSApriv*);
361 RSApub* X509toRSApub(uchar*, int, char*, int);
362 RSApub* asn1toRSApub(uchar*, int);
363 RSApriv* asn1toRSApriv(uchar*, int);
364 void asn1dump(uchar *der, int len);
365 uchar* decodePEM(char *s, char *type, int *len, char **new_s);
366 PEMChain* decodepemchain(char *s, char *type);
367 uchar* X509rsagen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
368 uchar* X509rsareq(RSApriv *priv, char *subj, int *certlen);
369 char* X509rsaverify(uchar *cert, int ncert, RSApub *pk);
370 char* X509rsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, RSApub *pk);
372 void X509dump(uchar *cert, int ncert);
374 mpint* pkcs1padbuf(uchar *buf, int len, mpint *modulus, int blocktype);
375 int pkcs1unpadbuf(uchar *buf, int len, mpint *modulus, int blocktype);
376 int asn1encodeRSApub(RSApub *pk, uchar *buf, int len);
377 int asn1encodedigest(DigestState* (*fun)(uchar*, ulong, uchar*, DigestState*),
378 uchar *digest, uchar *buf, int len);
384 typedef struct EGpub EGpub;
385 typedef struct EGpriv EGpriv;
386 typedef struct EGsig EGsig;
388 /* public/encryption key */
391 mpint *p; /* modulus */
392 mpint *alpha; /* generator */
393 mpint *key; /* (encryption key) alpha**secret mod p */
396 /* private/decryption key */
400 mpint *secret; /* (decryption key) */
409 EGpriv* eggen(int nlen, int rounds);
410 mpint* egencrypt(EGpub *k, mpint *in, mpint *out); /* deprecated */
411 mpint* egdecrypt(EGpriv *k, mpint *in, mpint *out);
412 EGsig* egsign(EGpriv *k, mpint *m);
413 int egverify(EGpub *k, EGsig *sig, mpint *m);
414 EGpub* egpuballoc(void);
415 void egpubfree(EGpub*);
416 EGpriv* egprivalloc(void);
417 void egprivfree(EGpriv*);
418 EGsig* egsigalloc(void);
419 void egsigfree(EGsig*);
420 EGpub* egprivtopub(EGpriv*);
425 typedef struct DSApub DSApub;
426 typedef struct DSApriv DSApriv;
427 typedef struct DSAsig DSAsig;
429 /* public/encryption key */
432 mpint *p; /* modulus */
433 mpint *q; /* group order, q divides p-1 */
434 mpint *alpha; /* group generator */
435 mpint *key; /* (encryption key) alpha**secret mod p */
438 /* private/decryption key */
442 mpint *secret; /* (decryption key) */
451 DSApriv* dsagen(DSApub *opub); /* opub not checked for consistency! */
452 DSAsig* dsasign(DSApriv *k, mpint *m);
453 int dsaverify(DSApub *k, DSAsig *sig, mpint *m);
454 DSApub* dsapuballoc(void);
455 void dsapubfree(DSApub*);
456 DSApriv* dsaprivalloc(void);
457 void dsaprivfree(DSApriv*);
458 DSAsig* dsasigalloc(void);
459 void dsasigfree(DSAsig*);
460 DSApub* dsaprivtopub(DSApriv*);
461 DSApriv* asn1toDSApriv(uchar*, int);
466 typedef struct Thumbprint{
467 struct Thumbprint *next;
468 uchar hash[SHA2_256dlen];
472 typedef struct TLSconn{
473 char dir[40]; /* connection directory */
474 uchar *cert; /* certificate (local on input, remote on output) */
480 int (*trace)(char*fmt, ...);
481 PEMChain*chain; /* optional extra certificate evidence for servers to present */
491 int tlsClient(int fd, TLSconn *c);
492 int tlsServer(int fd, TLSconn *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);
501 uchar *readcert(char *filename, int *pcertlen);
502 PEMChain*readcertchain(char *filename);
505 int aes_xts_encrypt(ulong tweak[], ulong ecb[], vlong sectorNumber, uchar *input, uchar *output, ulong len) ;
506 int aes_xts_decrypt(ulong tweak[], ulong ecb[], vlong sectorNumber, uchar *input, uchar *output, ulong len);
508 typedef struct ECpoint{
512 mpint *z; /* nil when using affine coordinates */
515 typedef ECpoint ECpub;
516 typedef struct ECpriv{
521 typedef struct ECdomain{
530 void ecdominit(ECdomain *, void (*init)(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h));
531 void ecdomfree(ECdomain *);
533 void ecassign(ECdomain *, ECpoint *old, ECpoint *new);
534 void ecadd(ECdomain *, ECpoint *a, ECpoint *b, ECpoint *s);
535 void ecmul(ECdomain *, ECpoint *a, mpint *k, ECpoint *s);
536 ECpoint* strtoec(ECdomain *, char *, char **, ECpoint *);
537 ECpriv* ecgen(ECdomain *, ECpriv*);
538 int ecverify(ECdomain *, ECpoint *);
539 int ecpubverify(ECdomain *, ECpub *);
540 void ecdsasign(ECdomain *, ECpriv *, uchar *, int, mpint *, mpint *);
541 int ecdsaverify(ECdomain *, ECpub *, uchar *, int, mpint *, mpint *);
542 void base58enc(uchar *, char *, int);
543 int base58dec(char *, uchar *, int);
545 ECpub* ecdecodepub(ECdomain *dom, uchar *, int);
546 int ecencodepub(ECdomain *dom, ECpub *, uchar *, int);
547 void ecpubfree(ECpub *);
549 ECpub* X509toECpub(uchar *cert, int ncert, char *name, int nname, ECdomain *dom);
550 char* X509ecdsaverify(uchar *cert, int ncert, ECdomain *dom, ECpub *pub);
551 char* X509ecdsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, ECdomain *dom, ECpub *pub);
554 void secp256r1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h);
555 void secp256k1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h);
556 void secp384r1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h);
558 DigestState* ripemd160(uchar *, ulong, uchar *, DigestState *);
561 * Diffie-Hellman key exchange
564 typedef struct DHstate DHstate;
567 mpint *g; /* base g */
568 mpint *p; /* large prime */
569 mpint *q; /* subgroup prime */
570 mpint *x; /* random secret */
571 mpint *y; /* public key y = g**x % p */
574 /* generate new public key: y = g**x % p */
575 mpint* dh_new(DHstate *dh, mpint *p, mpint *q, mpint *g);
577 /* calculate shared key: k = y**x % p */
578 mpint* dh_finish(DHstate *dh, mpint *y);
580 /* Curve25519 elliptic curve, public key function */
581 void curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]);
583 /* Curve25519 diffie hellman */
584 void curve25519_dh_new(uchar x[32], uchar y[32]);
585 void curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]);
587 /* password-based key derivation function 2 (rfc2898) */
588 void pbkdf2_x(uchar *p, ulong plen, uchar *s, ulong slen, ulong rounds, uchar *d, ulong dlen,
589 DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
591 /* scrypt password-based key derivation function */
592 char* scrypt(uchar *p, ulong plen, uchar *s, ulong slen,
593 ulong N, ulong R, ulong P,
594 uchar *d, ulong dlen);
596 /* hmac-based key derivation function (rfc5869) */
597 void hkdf_x(uchar *salt, ulong nsalt, uchar *info, ulong ninfo, uchar *key, ulong nkey, uchar *d, ulong dlen,
598 DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
600 /* timing safe memcmp() */
601 int tsmemcmp(void*, void*, ulong);