xref: /plan9/sys/include/libsec.h (revision f9e1cf08d3be51592e03e639fc848a68dc31a55e)
1 #pragma	lib	"libsec.a"
2 #pragma	src	"/sys/src/libsec"
3 
4 #ifndef _MPINT
5 typedef struct mpint mpint;
6 #endif
7 
8 /*
9  * AES definitions
10  */
11 
12 enum
13 {
14 	AESbsize=	16,
15 	AESmaxkey=	32,
16 	AESmaxrounds=	14
17 };
18 
19 typedef struct AESstate AESstate;
20 struct AESstate
21 {
22 	ulong	setup;
23 	int	rounds;
24 	int	keybytes;
25 	uchar	key[AESmaxkey];			/* unexpanded key */
26 	u32int	ekey[4*(AESmaxrounds + 1)];	/* encryption key */
27 	u32int	dkey[4*(AESmaxrounds + 1)];	/* decryption key */
28 	uchar	ivec[AESbsize];			/* initialization vector */
29 };
30 
31 void	setupAESstate(AESstate *s, uchar key[], int keybytes, uchar *ivec);
32 void	aesCBCencrypt(uchar *p, int len, AESstate *s);
33 void	aesCBCdecrypt(uchar *p, int len, AESstate *s);
34 
35 /*
36  * Blowfish Definitions
37  */
38 
39 enum
40 {
41 	BFbsize	= 8,
42 	BFrounds= 16
43 };
44 
45 /* 16-round Blowfish */
46 typedef struct BFstate BFstate;
47 struct BFstate
48 {
49 	ulong	setup;
50 
51 	uchar	key[56];
52 	uchar	ivec[8];
53 
54 	u32int 	pbox[BFrounds+2];
55 	u32int	sbox[1024];
56 };
57 
58 void	setupBFstate(BFstate *s, uchar key[], int keybytes, uchar *ivec);
59 void	bfCBCencrypt(uchar*, int, BFstate*);
60 void	bfCBCdecrypt(uchar*, int, BFstate*);
61 void	bfECBencrypt(uchar*, int, BFstate*);
62 void	bfECBdecrypt(uchar*, int, BFstate*);
63 
64 /*
65  * DES definitions
66  */
67 
68 enum
69 {
70 	DESbsize=	8
71 };
72 
73 /* single des */
74 typedef struct DESstate DESstate;
75 struct DESstate
76 {
77 	ulong	setup;
78 	uchar	key[8];		/* unexpanded key */
79 	ulong	expanded[32];	/* expanded key */
80 	uchar	ivec[8];	/* initialization vector */
81 };
82 
83 void	setupDESstate(DESstate *s, uchar key[8], uchar *ivec);
84 void	des_key_setup(uchar[8], ulong[32]);
85 void	block_cipher(ulong*, uchar*, int);
86 void	desCBCencrypt(uchar*, int, DESstate*);
87 void	desCBCdecrypt(uchar*, int, DESstate*);
88 void	desECBencrypt(uchar*, int, DESstate*);
89 void	desECBdecrypt(uchar*, int, DESstate*);
90 
91 /* for backward compatibility with 7-byte DES key format */
92 void	des56to64(uchar *k56, uchar *k64);
93 void	des64to56(uchar *k64, uchar *k56);
94 void	key_setup(uchar[7], ulong[32]);
95 
96 /* triple des encrypt/decrypt orderings */
97 enum {
98 	DES3E=		0,
99 	DES3D=		1,
100 	DES3EEE=	0,
101 	DES3EDE=	2,
102 	DES3DED=	5,
103 	DES3DDD=	7
104 };
105 
106 typedef struct DES3state DES3state;
107 struct DES3state
108 {
109 	ulong	setup;
110 	uchar	key[3][8];		/* unexpanded key */
111 	ulong	expanded[3][32];	/* expanded key */
112 	uchar	ivec[8];		/* initialization vector */
113 };
114 
115 void	setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec);
116 void	triple_block_cipher(ulong keys[3][32], uchar*, int);
117 void	des3CBCencrypt(uchar*, int, DES3state*);
118 void	des3CBCdecrypt(uchar*, int, DES3state*);
119 void	des3ECBencrypt(uchar*, int, DES3state*);
120 void	des3ECBdecrypt(uchar*, int, DES3state*);
121 
122 /*
123  * digests
124  */
125 
126 enum
127 {
128 	SHA1dlen=	20,	/* SHA digest length */
129 	MD4dlen=	16,	/* MD4 digest length */
130 	MD5dlen=	16,	/* MD5 digest length */
131 	AESdlen=	16,	/* TODO: see rfc */
132 
133 	Hmacblksz	= 64,	/* in bytes; from rfc2104 */
134 };
135 
136 typedef struct DigestState DigestState;
137 struct DigestState
138 {
139 	uvlong	len;
140 	u32int	state[5];
141 	uchar	buf[128];
142 	int	blen;
143 	char	malloced;
144 	char	seeded;
145 };
146 typedef struct DigestState SHAstate;	/* obsolete name */
147 typedef struct DigestState SHA1state;
148 typedef struct DigestState MD5state;
149 typedef struct DigestState MD4state;
150 typedef struct DigestState AEShstate;
151 
152 DigestState*	md4(uchar*, ulong, uchar*, DigestState*);
153 DigestState*	md5(uchar*, ulong, uchar*, DigestState*);
154 DigestState*	sha1(uchar*, ulong, uchar*, DigestState*);
155 DigestState*	aes(uchar*, ulong, uchar*, DigestState*);
156 DigestState*	hmac_x(uchar *p, ulong len, uchar *key, ulong klen,
157 			uchar *digest, DigestState *s,
158 			DigestState*(*x)(uchar*, ulong, uchar*, DigestState*),
159 			int xlen);
160 DigestState*	hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
161 DigestState*	hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
162 DigestState*	hmac_aes(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
163 char*		md5pickle(MD5state*);
164 MD5state*	md5unpickle(char*);
165 char*		sha1pickle(SHA1state*);
166 SHA1state*	sha1unpickle(char*);
167 
168 /*
169  * random number generation
170  */
171 void	genrandom(uchar *buf, int nbytes);
172 void	prng(uchar *buf, int nbytes);
173 ulong	fastrand(void);
174 ulong	nfastrand(ulong);
175 
176 /*
177  * primes
178  */
179 void	genprime(mpint *p, int n, int accuracy); /* generate n-bit probable prime */
180 void	gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); /* prime & generator */
181 void	genstrongprime(mpint *p, int n, int accuracy); /* generate n-bit strong prime */
182 void	DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
183 int	probably_prime(mpint *n, int nrep);	/* miller-rabin test */
184 int	smallprimetest(mpint *p);  /* returns -1 if not prime, 0 otherwise */
185 
186 /*
187  * rc4
188  */
189 typedef struct RC4state RC4state;
190 struct RC4state
191 {
192 	 uchar	state[256];
193 	 uchar	x;
194 	 uchar	y;
195 };
196 
197 void	setupRC4state(RC4state*, uchar*, int);
198 void	rc4(RC4state*, uchar*, int);
199 void	rc4skip(RC4state*, int);
200 void	rc4back(RC4state*, int);
201 
202 /*
203  * rsa
204  */
205 typedef struct RSApub RSApub;
206 typedef struct RSApriv RSApriv;
207 typedef struct PEMChain PEMChain;
208 
209 /* public/encryption key */
210 struct RSApub
211 {
212 	mpint	*n;	/* modulus */
213 	mpint	*ek;	/* exp (encryption key) */
214 };
215 
216 /* private/decryption key */
217 struct RSApriv
218 {
219 	RSApub	pub;
220 
221 	mpint	*dk;	/* exp (decryption key) */
222 
223 	/* precomputed values to help with chinese remainder theorem calc */
224 	mpint	*p;
225 	mpint	*q;
226 	mpint	*kp;	/* dk mod p-1 */
227 	mpint	*kq;	/* dk mod q-1 */
228 	mpint	*c2;	/* (inv p) mod q */
229 };
230 
231 struct PEMChain{
232 	PEMChain*next;
233 	uchar	*pem;
234 	int	pemlen;
235 };
236 
237 RSApriv*	rsagen(int nlen, int elen, int rounds);
238 RSApriv*	rsafill(mpint *n, mpint *e, mpint *d, mpint *p, mpint *q);
239 mpint*		rsaencrypt(RSApub *k, mpint *in, mpint *out);
240 mpint*		rsadecrypt(RSApriv *k, mpint *in, mpint *out);
241 RSApub*		rsapuballoc(void);
242 void		rsapubfree(RSApub*);
243 RSApriv*	rsaprivalloc(void);
244 void		rsaprivfree(RSApriv*);
245 RSApub*		rsaprivtopub(RSApriv*);
246 RSApub*		X509toRSApub(uchar*, int, char*, int);
247 RSApriv*	asn1toRSApriv(uchar*, int);
248 void		asn1dump(uchar *der, int len);
249 uchar*		decodePEM(char *s, char *type, int *len, char **new_s);
250 PEMChain*	decodepemchain(char *s, char *type);
251 uchar*		X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
252 uchar*		X509req(RSApriv *priv, char *subj, int *certlen);
253 char*		X509verify(uchar *cert, int ncert, RSApub *pk);
254 void		X509dump(uchar *cert, int ncert);
255 
256 /*
257  * elgamal
258  */
259 typedef struct EGpub EGpub;
260 typedef struct EGpriv EGpriv;
261 typedef struct EGsig EGsig;
262 
263 /* public/encryption key */
264 struct EGpub
265 {
266 	mpint	*p;	/* modulus */
267 	mpint	*alpha;	/* generator */
268 	mpint	*key;	/* (encryption key) alpha**secret mod p */
269 };
270 
271 /* private/decryption key */
272 struct EGpriv
273 {
274 	EGpub	pub;
275 	mpint	*secret;	/* (decryption key) */
276 };
277 
278 /* signature */
279 struct EGsig
280 {
281 	mpint	*r, *s;
282 };
283 
284 EGpriv*		eggen(int nlen, int rounds);
285 mpint*		egencrypt(EGpub *k, mpint *in, mpint *out);	/* deprecated */
286 mpint*		egdecrypt(EGpriv *k, mpint *in, mpint *out);
287 EGsig*		egsign(EGpriv *k, mpint *m);
288 int		egverify(EGpub *k, EGsig *sig, mpint *m);
289 EGpub*		egpuballoc(void);
290 void		egpubfree(EGpub*);
291 EGpriv*		egprivalloc(void);
292 void		egprivfree(EGpriv*);
293 EGsig*		egsigalloc(void);
294 void		egsigfree(EGsig*);
295 EGpub*		egprivtopub(EGpriv*);
296 
297 /*
298  * dsa
299  */
300 typedef struct DSApub DSApub;
301 typedef struct DSApriv DSApriv;
302 typedef struct DSAsig DSAsig;
303 
304 /* public/encryption key */
305 struct DSApub
306 {
307 	mpint	*p;	/* modulus */
308 	mpint	*q;	/* group order, q divides p-1 */
309 	mpint	*alpha;	/* group generator */
310 	mpint	*key;	/* (encryption key) alpha**secret mod p */
311 };
312 
313 /* private/decryption key */
314 struct DSApriv
315 {
316 	DSApub	pub;
317 	mpint	*secret;	/* (decryption key) */
318 };
319 
320 /* signature */
321 struct DSAsig
322 {
323 	mpint	*r, *s;
324 };
325 
326 DSApriv*	dsagen(DSApub *opub);	/* opub not checked for consistency! */
327 DSAsig*		dsasign(DSApriv *k, mpint *m);
328 int		dsaverify(DSApub *k, DSAsig *sig, mpint *m);
329 DSApub*		dsapuballoc(void);
330 void		dsapubfree(DSApub*);
331 DSApriv*	dsaprivalloc(void);
332 void		dsaprivfree(DSApriv*);
333 DSAsig*		dsasigalloc(void);
334 void		dsasigfree(DSAsig*);
335 DSApub*		dsaprivtopub(DSApriv*);
336 
337 /*
338  * TLS
339  */
340 typedef struct Thumbprint{
341 	struct Thumbprint *next;
342 	uchar	sha1[SHA1dlen];
343 } Thumbprint;
344 
345 typedef struct TLSconn{
346 	char	dir[40];	/* connection directory */
347 	uchar	*cert;	/* certificate (local on input, remote on output) */
348 	uchar	*sessionID;
349 	int	certlen;
350 	int	sessionIDlen;
351 	int	(*trace)(char*fmt, ...);
352 	PEMChain*chain;	/* optional extra certificate evidence for servers to present */
353 	char	*sessionType;
354 	uchar	*sessionKey;
355 	int	sessionKeylen;
356 	char	*sessionConst;
357 } TLSconn;
358 
359 /* tlshand.c */
360 int tlsClient(int fd, TLSconn *c);
361 int tlsServer(int fd, TLSconn *c);
362 
363 /* thumb.c */
364 Thumbprint* initThumbprints(char *ok, char *crl);
365 void	freeThumbprints(Thumbprint *ok);
366 int	okThumbprint(uchar *sha1, Thumbprint *ok);
367 
368 /* readcert.c */
369 uchar	*readcert(char *filename, int *pcertlen);
370 PEMChain*readcertchain(char *filename);
371