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 */
144 AESdlen= 16, /* TODO: see rfc */
146 Hmacblksz = 64, /* in bytes; from rfc2104 */
149 typedef struct DigestState DigestState;
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;
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*),
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*);
197 * random number generation
199 void genrandom(uchar *buf, int nbytes);
200 void prng(uchar *buf, int nbytes);
201 ulong fastrand(void);
202 ulong nfastrand(ulong);
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 */
217 typedef struct RC4state RC4state;
225 void setupRC4state(RC4state*, uchar*, int);
226 void rc4(RC4state*, uchar*, int);
227 void rc4skip(RC4state*, int);
228 void rc4back(RC4state*, int);
233 typedef struct RSApub RSApub;
234 typedef struct RSApriv RSApriv;
235 typedef struct PEMChain PEMChain;
237 /* public/encryption key */
240 mpint *n; /* modulus */
241 mpint *ek; /* exp (encryption key) */
244 /* private/decryption key */
249 mpint *dk; /* exp (decryption key) */
251 /* precomputed values to help with chinese remainder theorem calc */
254 mpint *kp; /* dk mod p-1 */
255 mpint *kq; /* dk mod q-1 */
256 mpint *c2; /* (inv p) mod q */
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);
287 typedef struct EGpub EGpub;
288 typedef struct EGpriv EGpriv;
289 typedef struct EGsig EGsig;
291 /* public/encryption key */
294 mpint *p; /* modulus */
295 mpint *alpha; /* generator */
296 mpint *key; /* (encryption key) alpha**secret mod p */
299 /* private/decryption key */
303 mpint *secret; /* (decryption key) */
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*);
328 typedef struct DSApub DSApub;
329 typedef struct DSApriv DSApriv;
330 typedef struct DSAsig DSAsig;
332 /* public/encryption key */
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 */
341 /* private/decryption key */
345 mpint *secret; /* (decryption key) */
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);
369 typedef struct Thumbprint{
370 struct Thumbprint *next;
371 uchar sha1[SHA1dlen];
374 typedef struct TLSconn{
375 char dir[40]; /* connection directory */
376 uchar *cert; /* certificate (local on input, remote on output) */
380 int (*trace)(char*fmt, ...);
381 PEMChain*chain; /* optional extra certificate evidence for servers to present */
390 int tlsClient(int fd, TLSconn *c);
391 int tlsServer(int fd, TLSconn *c);
394 Thumbprint* initThumbprints(char *ok, char *crl);
395 void freeThumbprints(Thumbprint *ok);
396 int okThumbprint(uchar *sha1, Thumbprint *ok);
399 uchar *readcert(char *filename, int *pcertlen);
400 PEMChain*readcertchain(char *filename);
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);
410 /* ids for ecnamedcurve */
416 typedef struct ECpoint{
422 typedef ECpoint ECpub;
423 typedef struct ECpriv{
428 typedef struct ECdomain{
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);
454 DigestState* ripemd160(uchar *, ulong, uchar *, DigestState *);
457 * Diffie-Hellman key exchange
460 typedef struct DHstate DHstate;
463 mpint *g; /* base g */
464 mpint *p; /* large prime */
465 mpint *x; /* random secret */
466 mpint *y; /* public key y = g ^ x % p */
469 /* generate new public key: y = g ^ x % p */
470 mpint* dh_new(DHstate *dh, mpint *p, mpint *g);
472 /* calculate shared key: k = pub ^ x % p */
473 mpint* dh_finish(DHstate *dh, mpint *pub);
475 /* constant-time comparison similar to memcmp(2) */
476 int constcmp(uchar *x, uchar *y, int len);
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);