Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature_unix/sys/include/ape/libsec.h – Rev 33

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#ifndef _PLAN9_SOURCE
2
  This header file is an extension to ANSI/POSIX
3
#endif
4
 
5
#ifndef __LIBSEC_H_
6
#define __LIBSEC_H_
33 7u83 7
 
2 - 8
#pragma	src	"/sys/src/ape/lib/sec"
9
#pragma	lib	"/$M/lib/ape/libsec.a"
10
 
33 7u83 11
#include <u.h>
12
 
2 - 13
#ifndef _MPINT
14
typedef struct mpint mpint;
15
#endif
16
 
17
/*
18
 * AES definitions
19
 */
20
 
21
enum
22
{
23
	AESbsize=	16,
24
	AESmaxkey=	32,
25
	AESmaxrounds=	14
26
};
27
 
28
typedef struct AESstate AESstate;
29
struct AESstate
30
{
31
	ulong	setup;
33 7u83 32
	ulong	offset;
2 - 33
	int	rounds;
34
	int	keybytes;
33 7u83 35
	void	*ekey;				/* expanded encryption round key */
36
	void	*dkey;				/* expanded decryption round key */
2 - 37
	uchar	key[AESmaxkey];			/* unexpanded key */
38
	uchar	ivec[AESbsize];			/* initialization vector */
33 7u83 39
	uchar	storage[512];			/* storage for expanded keys */
2 - 40
};
41
 
42
/* block ciphers */
33 7u83 43
extern void (*aes_encrypt)(ulong rk[], int Nr, uchar pt[16], uchar ct[16]);
44
extern void (*aes_decrypt)(ulong rk[], int Nr, uchar ct[16], uchar pt[16]);
2 - 45
 
33 7u83 46
void	setupAESstate(AESstate *s, uchar key[], int nkey, uchar *ivec);
47
 
2 - 48
void	aesCBCencrypt(uchar *p, int len, AESstate *s);
49
void	aesCBCdecrypt(uchar *p, int len, AESstate *s);
33 7u83 50
void	aesCFBencrypt(uchar *p, int len, AESstate *s);
51
void	aesCFBdecrypt(uchar *p, int len, AESstate *s);
52
void	aesOFBencrypt(uchar *p, int len, AESstate *s);
2 - 53
 
33 7u83 54
typedef struct AESGCMstate AESGCMstate;
55
struct AESGCMstate
56
{
57
	AESstate;
2 - 58
 
33 7u83 59
	ulong	H[4];
60
	ulong	M[16][256][4];
61
};
62
 
63
void	setupAESGCMstate(AESGCMstate *s, uchar *key, int keylen, uchar *iv, int ivlen);
64
void	aesgcm_setiv(AESGCMstate *s, uchar *iv, int ivlen);
65
void	aesgcm_encrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], AESGCMstate *s);
66
int	aesgcm_decrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], AESGCMstate *s);
67
 
2 - 68
/*
69
 * Blowfish Definitions
70
 */
71
 
72
enum
73
{
74
	BFbsize	= 8,
75
	BFrounds= 16
76
};
77
 
78
/* 16-round Blowfish */
79
typedef struct BFstate BFstate;
80
struct BFstate
81
{
82
	ulong	setup;
83
 
84
	uchar	key[56];
85
	uchar	ivec[8];
86
 
87
	u32int 	pbox[BFrounds+2];
88
	u32int	sbox[1024];
89
};
90
 
91
void	setupBFstate(BFstate *s, uchar key[], int keybytes, uchar *ivec);
92
void	bfCBCencrypt(uchar*, int, BFstate*);
93
void	bfCBCdecrypt(uchar*, int, BFstate*);
94
void	bfECBencrypt(uchar*, int, BFstate*);
95
void	bfECBdecrypt(uchar*, int, BFstate*);
96
 
97
/*
33 7u83 98
 * Chacha definitions
99
 */
100
 
101
enum
102
{
103
	ChachaBsize=	64,
104
	ChachaKeylen=	256/8,
105
	ChachaIVlen=	96/8,
106
	XChachaIVlen=	192/8,
107
};
108
 
109
typedef struct Chachastate Chachastate;
110
struct Chachastate
111
{
112
	union{
113
		u32int	input[16];
114
		struct {
115
			u32int	constant[4];
116
			u32int	key[8];
117
			u32int	counter;
118
			u32int	iv[3];
119
		};
120
	};
121
	u32int	xkey[8];
122
	int	rounds;
123
	int	ivwords;
124
};
125
 
126
void	setupChachastate(Chachastate*, uchar*, ulong, uchar*, ulong, int);
127
void	chacha_setiv(Chachastate *, uchar*);
128
void	chacha_setblock(Chachastate*, u64int);
129
void	chacha_encrypt(uchar*, ulong, Chachastate*);
130
void	chacha_encrypt2(uchar*, uchar*, ulong, Chachastate*);
131
 
132
void	hchacha(uchar h[32], uchar *key, ulong keylen, uchar nonce[16], int rounds);
133
 
134
void	ccpoly_encrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], Chachastate *cs);
135
int	ccpoly_decrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], Chachastate *cs);
136
 
137
/*
138
 * Salsa definitions
139
 */
140
enum
141
{
142
	SalsaBsize=	64,
143
	SalsaKeylen=	256/8,
144
	SalsaIVlen=	64/8,
145
	XSalsaIVlen=	192/8,
146
};
147
 
148
typedef struct Salsastate Salsastate;
149
struct Salsastate
150
{
151
	u32int	input[16];
152
	u32int	xkey[8];
153
	int	rounds;
154
	int	ivwords;
155
};
156
 
157
void	setupSalsastate(Salsastate*, uchar*, ulong, uchar*, ulong, int);
158
void	salsa_setiv(Salsastate*, uchar*);
159
void	salsa_setblock(Salsastate*, u64int);
160
void	salsa_encrypt(uchar*, ulong, Salsastate*);
161
void	salsa_encrypt2(uchar*, uchar*, ulong, Salsastate*);
162
 
163
void	salsa_core(u32int in[16], u32int out[16], int rounds);
164
 
165
void	hsalsa(uchar h[32], uchar *key, ulong keylen, uchar nonce[16], int rounds);
166
 
167
/*
2 - 168
 * DES definitions
169
 */
170
 
171
enum
172
{
173
	DESbsize=	8
174
};
175
 
176
/* single des */
177
typedef struct DESstate DESstate;
178
struct DESstate
179
{
180
	ulong	setup;
181
	uchar	key[8];		/* unexpanded key */
182
	ulong	expanded[32];	/* expanded key */
183
	uchar	ivec[8];	/* initialization vector */
184
};
185
 
186
void	setupDESstate(DESstate *s, uchar key[8], uchar *ivec);
187
void	des_key_setup(uchar[8], ulong[32]);
188
void	block_cipher(ulong*, uchar*, int);
189
void	desCBCencrypt(uchar*, int, DESstate*);
190
void	desCBCdecrypt(uchar*, int, DESstate*);
191
void	desECBencrypt(uchar*, int, DESstate*);
192
void	desECBdecrypt(uchar*, int, DESstate*);
193
 
194
/* for backward compatibility with 7-byte DES key format */
195
void	des56to64(uchar *k56, uchar *k64);
196
void	des64to56(uchar *k64, uchar *k56);
197
void	key_setup(uchar[7], ulong[32]);
198
 
199
/* triple des encrypt/decrypt orderings */
200
enum {
201
	DES3E=		0,
202
	DES3D=		1,
203
	DES3EEE=	0,
204
	DES3EDE=	2,
205
	DES3DED=	5,
206
	DES3DDD=	7
207
};
208
 
209
typedef struct DES3state DES3state;
210
struct DES3state
211
{
212
	ulong	setup;
213
	uchar	key[3][8];		/* unexpanded key */
214
	ulong	expanded[3][32];	/* expanded key */
215
	uchar	ivec[8];		/* initialization vector */
216
};
217
 
218
void	setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec);
219
void	triple_block_cipher(ulong keys[3][32], uchar*, int);
220
void	des3CBCencrypt(uchar*, int, DES3state*);
221
void	des3CBCdecrypt(uchar*, int, DES3state*);
222
void	des3ECBencrypt(uchar*, int, DES3state*);
223
void	des3ECBdecrypt(uchar*, int, DES3state*);
224
 
225
/*
226
 * digests
227
 */
228
 
229
enum
230
{
231
	SHA1dlen=	20,	/* SHA digest length */
232
	SHA2_224dlen=	28,	/* SHA-224 digest length */
233
	SHA2_256dlen=	32,	/* SHA-256 digest length */
234
	SHA2_384dlen=	48,	/* SHA-384 digest length */
235
	SHA2_512dlen=	64,	/* SHA-512 digest length */
236
	MD4dlen=	16,	/* MD4 digest length */
237
	MD5dlen=	16,	/* MD5 digest length */
33 7u83 238
	Poly1305dlen=	16,	/* Poly1305 digest length */
2 - 239
 
240
	Hmacblksz	= 64,	/* in bytes; from rfc2104 */
241
};
242
 
243
typedef struct DigestState DigestState;
244
struct DigestState
245
{
246
	uvlong	len;
247
	union {
33 7u83 248
		u32int	state[16];
2 - 249
		u64int	bstate[8];
250
	};
251
	uchar	buf[256];
252
	int	blen;
253
	char	malloced;
254
	char	seeded;
255
};
256
typedef struct DigestState SHAstate;	/* obsolete name */
257
typedef struct DigestState SHA1state;
258
typedef struct DigestState SHA2_224state;
259
typedef struct DigestState SHA2_256state;
260
typedef struct DigestState SHA2_384state;
261
typedef struct DigestState SHA2_512state;
262
typedef struct DigestState MD5state;
263
typedef struct DigestState MD4state;
264
 
265
DigestState*	md4(uchar*, ulong, uchar*, DigestState*);
266
DigestState*	md5(uchar*, ulong, uchar*, DigestState*);
267
DigestState*	sha1(uchar*, ulong, uchar*, DigestState*);
268
DigestState*	sha2_224(uchar*, ulong, uchar*, DigestState*);
269
DigestState*	sha2_256(uchar*, ulong, uchar*, DigestState*);
270
DigestState*	sha2_384(uchar*, ulong, uchar*, DigestState*);
271
DigestState*	sha2_512(uchar*, ulong, uchar*, DigestState*);
272
DigestState*	hmac_x(uchar *p, ulong len, uchar *key, ulong klen,
273
			uchar *digest, DigestState *s,
274
			DigestState*(*x)(uchar*, ulong, uchar*, DigestState*),
275
			int xlen);
276
DigestState*	hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
277
DigestState*	hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
278
DigestState*	hmac_sha2_224(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
279
DigestState*	hmac_sha2_256(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
280
DigestState*	hmac_sha2_384(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
281
DigestState*	hmac_sha2_512(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
282
char*		md5pickle(MD5state*);
283
MD5state*	md5unpickle(char*);
284
char*		sha1pickle(SHA1state*);
285
SHA1state*	sha1unpickle(char*);
286
 
33 7u83 287
DigestState*	poly1305(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
288
 
2 - 289
/*
290
 * random number generation
291
 */
292
void	genrandom(uchar *buf, int nbytes);
293
void	prng(uchar *buf, int nbytes);
294
ulong	fastrand(void);
295
ulong	nfastrand(ulong);
296
 
297
/*
298
 * primes
299
 */
300
void	genprime(mpint *p, int n, int accuracy); /* generate n-bit probable prime */
301
void	gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); /* prime & generator */
302
void	genstrongprime(mpint *p, int n, int accuracy); /* generate n-bit strong prime */
303
void	DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
304
int	probably_prime(mpint *n, int nrep);	/* miller-rabin test */
305
int	smallprimetest(mpint *p);  /* returns -1 if not prime, 0 otherwise */
306
 
307
/*
308
 * rc4
309
 */
310
typedef struct RC4state RC4state;
311
struct RC4state
312
{
313
	 uchar	state[256];
314
	 uchar	x;
315
	 uchar	y;
316
};
317
 
318
void	setupRC4state(RC4state*, uchar*, int);
319
void	rc4(RC4state*, uchar*, int);
320
void	rc4skip(RC4state*, int);
321
void	rc4back(RC4state*, int);
322
 
323
/*
324
 * rsa
325
 */
326
typedef struct RSApub RSApub;
327
typedef struct RSApriv RSApriv;
328
typedef struct PEMChain PEMChain;
329
 
330
/* public/encryption key */
331
struct RSApub
332
{
333
	mpint	*n;	/* modulus */
334
	mpint	*ek;	/* exp (encryption key) */
335
};
336
 
337
/* private/decryption key */
338
struct RSApriv
339
{
340
	RSApub	pub;
341
 
342
	mpint	*dk;	/* exp (decryption key) */
343
 
344
	/* precomputed values to help with chinese remainder theorem calc */
345
	mpint	*p;
346
	mpint	*q;
347
	mpint	*kp;	/* dk mod p-1 */
348
	mpint	*kq;	/* dk mod q-1 */
349
	mpint	*c2;	/* (inv p) mod q */
350
};
351
 
352
struct PEMChain{
353
	PEMChain*next;
354
	uchar	*pem;
355
	int	pemlen;
356
};
357
 
358
RSApriv*	rsagen(int nlen, int elen, int rounds);
359
RSApriv*	rsafill(mpint *n, mpint *e, mpint *d, mpint *p, mpint *q);
360
mpint*		rsaencrypt(RSApub *k, mpint *in, mpint *out);
361
mpint*		rsadecrypt(RSApriv *k, mpint *in, mpint *out);
362
RSApub*		rsapuballoc(void);
363
void		rsapubfree(RSApub*);
364
RSApriv*	rsaprivalloc(void);
365
void		rsaprivfree(RSApriv*);
366
RSApub*		rsaprivtopub(RSApriv*);
367
RSApub*		X509toRSApub(uchar*, int, char*, int);
368
RSApriv*	asn1toRSApriv(uchar*, int);
33 7u83 369
RSApub*		asn1toRSApub(uchar*, int);
2 - 370
void		asn1dump(uchar *der, int len);
371
uchar*		decodePEM(char *s, char *type, int *len, char **new_s);
372
PEMChain*	decodepemchain(char *s, char *type);
33 7u83 373
uchar*		X509rsagen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
374
uchar*		X509rsareq(RSApriv *priv, char *subj, int *certlen);
375
char*		X509rsaverify(uchar *cert, int ncert, RSApub *pk);
376
char*		X509rsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, RSApub *pk);
377
 
2 - 378
void		X509dump(uchar *cert, int ncert);
379
 
33 7u83 380
mpint*		pkcs1padbuf(uchar *buf, int len, mpint *modulus, int blocktype);
381
int		pkcs1unpadbuf(uchar *buf, int len, mpint *modulus, int blocktype);
382
int		asn1encodeRSApub(RSApub *pk, uchar *buf, int len);
383
int		asn1encodedigest(DigestState* (*fun)(uchar*, ulong, uchar*, DigestState*),
384
			uchar *digest, uchar *buf, int len);
385
 
386
int		X509digestSPKI(uchar *, int, DigestState* (*)(uchar*, ulong, uchar*, DigestState*), uchar *);
387
 
2 - 388
/*
389
 * elgamal
390
 */
391
typedef struct EGpub EGpub;
392
typedef struct EGpriv EGpriv;
393
typedef struct EGsig EGsig;
394
 
395
/* public/encryption key */
396
struct EGpub
397
{
398
	mpint	*p;	/* modulus */
399
	mpint	*alpha;	/* generator */
400
	mpint	*key;	/* (encryption key) alpha**secret mod p */
401
};
402
 
403
/* private/decryption key */
404
struct EGpriv
405
{
406
	EGpub	pub;
407
	mpint	*secret;	/* (decryption key) */
408
};
409
 
410
/* signature */
411
struct EGsig
412
{
413
	mpint	*r, *s;
414
};
415
 
416
EGpriv*		eggen(int nlen, int rounds);
417
mpint*		egencrypt(EGpub *k, mpint *in, mpint *out);	/* deprecated */
418
mpint*		egdecrypt(EGpriv *k, mpint *in, mpint *out);
419
EGsig*		egsign(EGpriv *k, mpint *m);
420
int		egverify(EGpub *k, EGsig *sig, mpint *m);
421
EGpub*		egpuballoc(void);
422
void		egpubfree(EGpub*);
423
EGpriv*		egprivalloc(void);
424
void		egprivfree(EGpriv*);
425
EGsig*		egsigalloc(void);
426
void		egsigfree(EGsig*);
427
EGpub*		egprivtopub(EGpriv*);
428
 
429
/*
430
 * dsa
431
 */
432
typedef struct DSApub DSApub;
433
typedef struct DSApriv DSApriv;
434
typedef struct DSAsig DSAsig;
435
 
436
/* public/encryption key */
437
struct DSApub
438
{
439
	mpint	*p;	/* modulus */
440
	mpint	*q;	/* group order, q divides p-1 */
441
	mpint	*alpha;	/* group generator */
442
	mpint	*key;	/* (encryption key) alpha**secret mod p */
443
};
444
 
445
/* private/decryption key */
446
struct DSApriv
447
{
448
	DSApub	pub;
449
	mpint	*secret;	/* (decryption key) */
450
};
451
 
452
/* signature */
453
struct DSAsig
454
{
455
	mpint	*r, *s;
456
};
457
 
458
DSApriv*	dsagen(DSApub *opub);	/* opub not checked for consistency! */
459
DSAsig*		dsasign(DSApriv *k, mpint *m);
460
int		dsaverify(DSApub *k, DSAsig *sig, mpint *m);
461
DSApub*		dsapuballoc(void);
462
void		dsapubfree(DSApub*);
463
DSApriv*	dsaprivalloc(void);
464
void		dsaprivfree(DSApriv*);
465
DSAsig*		dsasigalloc(void);
466
void		dsasigfree(DSAsig*);
467
DSApub*		dsaprivtopub(DSApriv*);
468
 
469
/*
470
 * TLS
471
 */
472
typedef struct Thumbprint{
473
	struct Thumbprint *next;
33 7u83 474
	uchar	hash[SHA2_256dlen];
475
	uchar	len;
2 - 476
} Thumbprint;
477
 
478
typedef struct TLSconn{
479
	char	dir[40];	/* connection directory */
480
	uchar	*cert;	/* certificate (local on input, remote on output) */
481
	uchar	*sessionID;
33 7u83 482
	uchar	*psk;
2 - 483
	int	certlen;
484
	int	sessionIDlen;
33 7u83 485
	int	psklen;
2 - 486
	int	(*trace)(char*fmt, ...);
487
	PEMChain*chain;	/* optional extra certificate evidence for servers to present */
488
	char	*sessionType;
489
	uchar	*sessionKey;
490
	int	sessionKeylen;
491
	char	*sessionConst;
33 7u83 492
	char	*serverName;
493
	char	*pskID;
2 - 494
} TLSconn;
495
 
496
/* tlshand.c */
497
int tlsClient(int fd, TLSconn *c);
498
int tlsServer(int fd, TLSconn *c);
499
 
500
/* thumb.c */
33 7u83 501
Thumbprint* initThumbprints(char *ok, char *crl, char *tag);
2 - 502
void	freeThumbprints(Thumbprint *ok);
33 7u83 503
int	okThumbprint(uchar *hash, int len, Thumbprint *ok);
504
int	okCertificate(uchar *cert, int len, Thumbprint *ok);
2 - 505
 
506
/* readcert.c */
507
uchar	*readcert(char *filename, int *pcertlen);
508
PEMChain*readcertchain(char *filename);
509
 
33 7u83 510
/* aes_xts.c */
511
void aes_xts_encrypt(AESstate *tweak, AESstate *ecb, uvlong sectorNumber, uchar *input, uchar *output, ulong len);
512
void aes_xts_decrypt(AESstate *tweak, AESstate *ecb, uvlong sectorNumber, uchar *input, uchar *output, ulong len);
513
 
514
typedef struct ECpoint{
515
	int inf;
516
	mpint *x;
517
	mpint *y;
518
	mpint *z;	/* nil when using affine coordinates */
519
} ECpoint;
520
 
521
typedef ECpoint ECpub;
522
typedef struct ECpriv{
523
	ECpoint;
524
	mpint *d;
525
} ECpriv;
526
 
527
typedef struct ECdomain{
528
	mpint *p;
529
	mpint *a;
530
	mpint *b;
531
	ECpoint G;
532
	mpint *n;
533
	mpint *h;
534
} ECdomain;
535
 
536
void	ecdominit(ECdomain *, void (*init)(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h));
537
void	ecdomfree(ECdomain *);
538
 
539
void	ecassign(ECdomain *, ECpoint *old, ECpoint *new);
540
void	ecadd(ECdomain *, ECpoint *a, ECpoint *b, ECpoint *s);
541
void	ecmul(ECdomain *, ECpoint *a, mpint *k, ECpoint *s);
542
ECpoint*	strtoec(ECdomain *, char *, char **, ECpoint *);
543
ECpriv*	ecgen(ECdomain *, ECpriv*);
544
int	ecverify(ECdomain *, ECpoint *);
545
int	ecpubverify(ECdomain *, ECpub *);
546
void	ecdsasign(ECdomain *, ECpriv *, uchar *, int, mpint *, mpint *);
547
int	ecdsaverify(ECdomain *, ECpub *, uchar *, int, mpint *, mpint *);
548
void	base58enc(uchar *, char *, int);
549
int	base58dec(char *, uchar *, int);
550
 
551
ECpub*	ecdecodepub(ECdomain *dom, uchar *, int);
552
int	ecencodepub(ECdomain *dom, ECpub *, uchar *, int);
553
void	ecpubfree(ECpub *);
554
 
555
ECpub*	X509toECpub(uchar *cert, int ncert, char *name, int nname, ECdomain *dom);
556
char*	X509ecdsaverify(uchar *cert, int ncert, ECdomain *dom, ECpub *pub);
557
char*	X509ecdsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, ECdomain *dom, ECpub *pub);
558
 
559
/* curves */
560
void	secp256r1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h);
561
void	secp256k1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h);
562
void	secp384r1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h);
563
 
564
DigestState*	ripemd160(uchar *, ulong, uchar *, DigestState *);
565
 
566
/*
567
 * Diffie-Hellman key exchange
568
 */
569
 
570
typedef struct DHstate DHstate;
571
struct DHstate
572
{
573
	mpint	*g;	/* base g */
574
	mpint	*p;	/* large prime */
575
	mpint	*q;	/* subgroup prime */
576
	mpint	*x;	/* random secret */
577
	mpint	*y;	/* public key y = g**x % p */
578
};
579
 
580
/* generate new public key: y = g**x % p */
581
mpint* dh_new(DHstate *dh, mpint *p, mpint *q, mpint *g);
582
 
583
/* calculate shared key: k = y**x % p */
584
mpint* dh_finish(DHstate *dh, mpint *y);
585
 
586
/* Curve25519 elliptic curve, public key function */
587
void curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]);
588
 
589
/* Curve25519 diffie hellman */
590
void curve25519_dh_new(uchar x[32], uchar y[32]);
591
void curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]);
592
 
593
/* password-based key derivation function 2 (rfc2898) */
594
void pbkdf2_x(uchar *p, ulong plen, uchar *s, ulong slen, ulong rounds, uchar *d, ulong dlen,
595
	DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
596
 
597
/* scrypt password-based key derivation function */
598
char* scrypt(uchar *p, ulong plen, uchar *s, ulong slen,
599
	ulong N, ulong R, ulong P,
600
	uchar *d, ulong dlen);
601
 
602
/* hmac-based key derivation function (rfc5869) */
603
void hkdf_x(uchar *salt, ulong nsalt, uchar *info, ulong ninfo, uchar *key, ulong nkey, uchar *d, ulong dlen,
604
	DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
605
 
606
/* timing safe memcmp() */
607
int tsmemcmp(void*, void*, ulong);
608
 
2 - 609
#endif