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