2 #pragma src "/sys/src/libsec"
6 typedef struct mpint mpint;
20 typedef struct AESstate AESstate;
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 */
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]);
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);
41 void setupAESXCBCstate(AESstate *s);
42 uchar* aesXCBCmac(uchar *p, int len, AESstate *s);
45 * Blowfish Definitions
54 /* 16-round Blowfish */
55 typedef struct BFstate BFstate;
63 u32int pbox[BFrounds+2];
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*);
83 typedef struct DESstate DESstate;
87 uchar key[8]; /* unexpanded key */
88 ulong expanded[32]; /* expanded key */
89 uchar ivec[8]; /* initialization vector */
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*);
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]);
105 /* triple des encrypt/decrypt orderings */
115 typedef struct DES3state DES3state;
119 uchar key[3][8]; /* unexpanded key */
120 ulong expanded[3][32]; /* expanded key */
121 uchar ivec[8]; /* initialization vector */
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*);
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 */
145 Hmacblksz = 64, /* in bytes; from rfc2104 */
148 typedef struct DigestState DigestState;
161 typedef struct DigestState SHAstate; /* obsolete name */
162 typedef struct DigestState SHA1state;
163 typedef struct DigestState SHA2_224state;
164 typedef struct DigestState SHA2_256state;
165 typedef struct DigestState SHA2_384state;
166 typedef struct DigestState SHA2_512state;
167 typedef struct DigestState MD5state;
168 typedef struct DigestState MD4state;
170 DigestState* md4(uchar*, ulong, uchar*, DigestState*);
171 DigestState* md5(uchar*, ulong, uchar*, DigestState*);
172 DigestState* sha1(uchar*, ulong, uchar*, DigestState*);
173 DigestState* sha2_224(uchar*, ulong, uchar*, DigestState*);
174 DigestState* sha2_256(uchar*, ulong, uchar*, DigestState*);
175 DigestState* sha2_384(uchar*, ulong, uchar*, DigestState*);
176 DigestState* sha2_512(uchar*, ulong, uchar*, DigestState*);
177 DigestState* hmac_x(uchar *p, ulong len, uchar *key, ulong klen,
178 uchar *digest, DigestState *s,
179 DigestState*(*x)(uchar*, ulong, uchar*, DigestState*),
181 DigestState* hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
182 DigestState* hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
183 DigestState* hmac_sha2_224(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
184 DigestState* hmac_sha2_256(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
185 DigestState* hmac_sha2_384(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
186 DigestState* hmac_sha2_512(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
187 char* md5pickle(MD5state*);
188 MD5state* md5unpickle(char*);
189 char* sha1pickle(SHA1state*);
190 SHA1state* sha1unpickle(char*);
193 * random number generation
195 void genrandom(uchar *buf, int nbytes);
196 void prng(uchar *buf, int nbytes);
197 ulong fastrand(void);
198 ulong nfastrand(ulong);
203 void genprime(mpint *p, int n, int accuracy); /* generate n-bit probable prime */
204 void gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); /* prime & generator */
205 void genstrongprime(mpint *p, int n, int accuracy); /* generate n-bit strong prime */
206 void DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
207 int probably_prime(mpint *n, int nrep); /* miller-rabin test */
208 int smallprimetest(mpint *p); /* returns -1 if not prime, 0 otherwise */
213 typedef struct RC4state RC4state;
221 void setupRC4state(RC4state*, uchar*, int);
222 void rc4(RC4state*, uchar*, int);
223 void rc4skip(RC4state*, int);
224 void rc4back(RC4state*, int);
229 typedef struct RSApub RSApub;
230 typedef struct RSApriv RSApriv;
231 typedef struct PEMChain PEMChain;
233 /* public/encryption key */
236 mpint *n; /* modulus */
237 mpint *ek; /* exp (encryption key) */
240 /* private/decryption key */
245 mpint *dk; /* exp (decryption key) */
247 /* precomputed values to help with chinese remainder theorem calc */
250 mpint *kp; /* dk mod p-1 */
251 mpint *kq; /* dk mod q-1 */
252 mpint *c2; /* (inv p) mod q */
261 RSApriv* rsagen(int nlen, int elen, int rounds);
262 RSApriv* rsafill(mpint *n, mpint *e, mpint *d, mpint *p, mpint *q);
263 mpint* rsaencrypt(RSApub *k, mpint *in, mpint *out);
264 mpint* rsadecrypt(RSApriv *k, mpint *in, mpint *out);
265 RSApub* rsapuballoc(void);
266 void rsapubfree(RSApub*);
267 RSApriv* rsaprivalloc(void);
268 void rsaprivfree(RSApriv*);
269 RSApub* rsaprivtopub(RSApriv*);
270 RSApub* X509toRSApub(uchar*, int, char*, int);
271 RSApriv* asn1toRSApriv(uchar*, int);
272 void asn1dump(uchar *der, int len);
273 uchar* decodePEM(char *s, char *type, int *len, char **new_s);
274 PEMChain* decodepemchain(char *s, char *type);
275 uchar* X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
276 uchar* X509req(RSApriv *priv, char *subj, int *certlen);
277 char* X509verifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, RSApub *pk);
278 char* X509verifydata(uchar *sig, int siglen, uchar *data, int datalen, RSApub *pk);
279 char* X509verify(uchar *cert, int ncert, RSApub *pk);
280 void X509dump(uchar *cert, int ncert);
285 typedef struct EGpub EGpub;
286 typedef struct EGpriv EGpriv;
287 typedef struct EGsig EGsig;
289 /* public/encryption key */
292 mpint *p; /* modulus */
293 mpint *alpha; /* generator */
294 mpint *key; /* (encryption key) alpha**secret mod p */
297 /* private/decryption key */
301 mpint *secret; /* (decryption key) */
310 EGpriv* eggen(int nlen, int rounds);
311 mpint* egencrypt(EGpub *k, mpint *in, mpint *out); /* deprecated */
312 mpint* egdecrypt(EGpriv *k, mpint *in, mpint *out);
313 EGsig* egsign(EGpriv *k, mpint *m);
314 int egverify(EGpub *k, EGsig *sig, mpint *m);
315 EGpub* egpuballoc(void);
316 void egpubfree(EGpub*);
317 EGpriv* egprivalloc(void);
318 void egprivfree(EGpriv*);
319 EGsig* egsigalloc(void);
320 void egsigfree(EGsig*);
321 EGpub* egprivtopub(EGpriv*);
326 typedef struct DSApub DSApub;
327 typedef struct DSApriv DSApriv;
328 typedef struct DSAsig DSAsig;
330 /* public/encryption key */
333 mpint *p; /* modulus */
334 mpint *q; /* group order, q divides p-1 */
335 mpint *alpha; /* group generator */
336 mpint *key; /* (encryption key) alpha**secret mod p */
339 /* private/decryption key */
343 mpint *secret; /* (decryption key) */
352 DSApriv* dsagen(DSApub *opub); /* opub not checked for consistency! */
353 DSAsig* dsasign(DSApriv *k, mpint *m);
354 int dsaverify(DSApub *k, DSAsig *sig, mpint *m);
355 DSApub* dsapuballoc(void);
356 void dsapubfree(DSApub*);
357 DSApriv* dsaprivalloc(void);
358 void dsaprivfree(DSApriv*);
359 DSAsig* dsasigalloc(void);
360 void dsasigfree(DSAsig*);
361 DSApub* dsaprivtopub(DSApriv*);
362 DSApriv* asn1toDSApriv(uchar*, int);
367 typedef struct Thumbprint{
368 struct Thumbprint *next;
369 uchar sha1[SHA1dlen];
372 typedef struct TLSconn{
373 char dir[40]; /* connection directory */
374 uchar *cert; /* certificate (local on input, remote on output) */
378 int (*trace)(char*fmt, ...);
379 PEMChain*chain; /* optional extra certificate evidence for servers to present */
388 int tlsClient(int fd, TLSconn *c);
389 int tlsServer(int fd, TLSconn *c);
392 Thumbprint* initThumbprints(char *ok, char *crl);
393 void freeThumbprints(Thumbprint *ok);
394 int okThumbprint(uchar *sha1, Thumbprint *ok);
397 uchar *readcert(char *filename, int *pcertlen);
398 PEMChain*readcertchain(char *filename);
401 int aes_xts_encrypt(ulong tweak[], ulong ecb[], vlong sectorNumber, uchar *input, uchar *output, ulong len) ;
402 int aes_xts_decrypt(ulong tweak[], ulong ecb[], vlong sectorNumber, uchar *input, uchar *output, ulong len);
404 typedef struct ECpoint{
410 typedef ECpoint ECpub;
411 typedef struct ECpriv{
416 typedef struct ECdomain{
425 void ecassign(ECdomain *, ECpoint *old, ECpoint *new);
426 void ecadd(ECdomain *, ECpoint *a, ECpoint *b, ECpoint *s);
427 void ecmul(ECdomain *, ECpoint *a, mpint *k, ECpoint *s);
428 ECpoint* strtoec(ECdomain *, char *, char **, ECpoint *);
429 ECpriv* ecgen(ECdomain *, ECpriv*);
430 int ecverify(ECdomain *, ECpoint *);
431 int ecpubverify(ECdomain *, ECpub *);
432 void ecdsasign(ECdomain *, ECpriv *, uchar *, int, mpint *, mpint *);
433 int ecdsaverify(ECdomain *, ECpub *, uchar *, int, mpint *, mpint *);
434 void base58enc(uchar *, char *, int);
435 int base58dec(char *, uchar *, int);
437 DigestState* ripemd160(uchar *, ulong, uchar *, DigestState *);
440 * Diffie-Hellman key exchange
443 typedef struct DHstate DHstate;
446 mpint *g; /* base g */
447 mpint *p; /* large prime */
448 mpint *q; /* subgroup prime */
449 mpint *x; /* random secret */
450 mpint *y; /* public key y = g**x % p */
453 /* generate new public key: y = g**x % p */
454 mpint* dh_new(DHstate *dh, mpint *p, mpint *q, mpint *g);
456 /* calculate shared key: k = y**x % p */
457 mpint* dh_finish(DHstate *dh, mpint *y);
459 /* Curve25519 elliptic curve, public key function */
460 void curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]);
462 /* Curve25519 diffie hellman */
463 void curve25519_dh_new(uchar x[32], uchar y[32]);
464 void curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]);
466 /* password-based key derivation function 2 (rfc2898) */
467 void pbkdf2_x(uchar *p, ulong plen, uchar *s, ulong slen, ulong rounds, uchar *d, ulong dlen,
468 DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
470 /* hmac-based key derivation function (rfc5869) */
471 void hkdf_x(uchar *salt, ulong nsalt, uchar *info, ulong ninfo, uchar *key, ulong nkey, uchar *d, ulong dlen,
472 DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);