xref: /plan9/sys/include/libsec.h (revision 282e677fa45fb578cdb8bc2c412ac084c367776e)
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 };
132 
133 typedef struct DigestState DigestState;
134 struct DigestState
135 {
136 	uvlong len;
137 	u32int state[5];
138 	uchar buf[128];
139 	int blen;
140 	char malloced;
141 	char seeded;
142 };
143 typedef struct DigestState SHAstate;	/* obsolete name */
144 typedef struct DigestState SHA1state;
145 typedef struct DigestState MD5state;
146 typedef struct DigestState MD4state;
147 
148 DigestState* md4(uchar*, ulong, uchar*, DigestState*);
149 DigestState* md5(uchar*, ulong, uchar*, DigestState*);
150 DigestState* sha1(uchar*, ulong, uchar*, DigestState*);
151 DigestState* hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
152 DigestState* hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
153 char* md5pickle(MD5state*);
154 MD5state* md5unpickle(char*);
155 char* sha1pickle(SHA1state*);
156 SHA1state* sha1unpickle(char*);
157 
158 /////////////////////////////////////////////////////////
159 // random number generation
160 /////////////////////////////////////////////////////////
161 void	genrandom(uchar *buf, int nbytes);
162 void	prng(uchar *buf, int nbytes);
163 ulong	fastrand(void);
164 ulong	nfastrand(ulong);
165 
166 /////////////////////////////////////////////////////////
167 // primes
168 /////////////////////////////////////////////////////////
169 void	genprime(mpint *p, int n, int accuracy); // generate an n bit probable prime
170 void	gensafeprime(mpint *p, mpint *alpha, int n, int accuracy);	// prime and generator
171 void	genstrongprime(mpint *p, int n, int accuracy);	// generate an n bit strong prime
172 void	DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
173 int	probably_prime(mpint *n, int nrep);	// miller-rabin test
174 int	smallprimetest(mpint *p);		// returns -1 if not prime, 0 otherwise
175 
176 /////////////////////////////////////////////////////////
177 // rc4
178 /////////////////////////////////////////////////////////
179 typedef struct RC4state RC4state;
180 struct RC4state
181 {
182 	 uchar state[256];
183 	 uchar x;
184 	 uchar y;
185 };
186 
187 void	setupRC4state(RC4state*, uchar*, int);
188 void	rc4(RC4state*, uchar*, int);
189 void	rc4skip(RC4state*, int);
190 void	rc4back(RC4state*, int);
191 
192 /////////////////////////////////////////////////////////
193 // rsa
194 /////////////////////////////////////////////////////////
195 typedef struct RSApub RSApub;
196 typedef struct RSApriv RSApriv;
197 typedef struct PEMChain PEMChain;
198 
199 // public/encryption key
200 struct RSApub
201 {
202 	mpint	*n;	// modulus
203 	mpint	*ek;	// exp (encryption key)
204 };
205 
206 // private/decryption key
207 struct RSApriv
208 {
209 	RSApub	pub;
210 
211 	mpint	*dk;	// exp (decryption key)
212 
213 	// precomputed values to help with chinese remainder theorem calc
214 	mpint	*p;
215 	mpint	*q;
216 	mpint	*kp;	// dk mod p-1
217 	mpint	*kq;	// dk mod q-1
218 	mpint	*c2;	// (inv p) mod q
219 };
220 
221 struct PEMChain{
222 	PEMChain *next;
223 	uchar *pem;
224 	int pemlen;
225 };
226 
227 RSApriv*	rsagen(int nlen, int elen, int rounds);
228 RSApriv*	rsafill(mpint *n, mpint *e, mpint *d, mpint *p, mpint *q);
229 mpint*		rsaencrypt(RSApub *k, mpint *in, mpint *out);
230 mpint*		rsadecrypt(RSApriv *k, mpint *in, mpint *out);
231 RSApub*		rsapuballoc(void);
232 void		rsapubfree(RSApub*);
233 RSApriv*	rsaprivalloc(void);
234 void		rsaprivfree(RSApriv*);
235 RSApub*		rsaprivtopub(RSApriv*);
236 RSApub*		X509toRSApub(uchar*, int, char*, int);
237 RSApriv*	asn1toRSApriv(uchar*, int);
238 void		asn1dump(uchar *der, int len);
239 uchar*		decodePEM(char *s, char *type, int *len, char **new_s);
240 PEMChain*	decodepemchain(char *s, char *type);
241 uchar*		X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
242 uchar*		X509req(RSApriv *priv, char *subj, int *certlen);
243 char*		X509verify(uchar *cert, int ncert, RSApub *pk);
244 void		X509dump(uchar *cert, int ncert);
245 
246 /////////////////////////////////////////////////////////
247 // elgamal
248 /////////////////////////////////////////////////////////
249 typedef struct EGpub EGpub;
250 typedef struct EGpriv EGpriv;
251 typedef struct EGsig EGsig;
252 
253 // public/encryption key
254 struct EGpub
255 {
256 	mpint	*p;	// modulus
257 	mpint	*alpha;	// generator
258 	mpint	*key;	// (encryption key) alpha**secret mod p
259 };
260 
261 // private/decryption key
262 struct EGpriv
263 {
264 	EGpub	pub;
265 	mpint	*secret; // (decryption key)
266 };
267 
268 // signature
269 struct EGsig
270 {
271 	mpint	*r, *s;
272 };
273 
274 EGpriv*		eggen(int nlen, int rounds);
275 mpint*		egencrypt(EGpub *k, mpint *in, mpint *out);	// deprecated
276 mpint*		egdecrypt(EGpriv *k, mpint *in, mpint *out);
277 EGsig*		egsign(EGpriv *k, mpint *m);
278 int		egverify(EGpub *k, EGsig *sig, mpint *m);
279 EGpub*		egpuballoc(void);
280 void		egpubfree(EGpub*);
281 EGpriv*		egprivalloc(void);
282 void		egprivfree(EGpriv*);
283 EGsig*		egsigalloc(void);
284 void		egsigfree(EGsig*);
285 EGpub*		egprivtopub(EGpriv*);
286 
287 /////////////////////////////////////////////////////////
288 // dsa
289 /////////////////////////////////////////////////////////
290 typedef struct DSApub DSApub;
291 typedef struct DSApriv DSApriv;
292 typedef struct DSAsig DSAsig;
293 
294 // public/encryption key
295 struct DSApub
296 {
297 	mpint	*p;	// modulus
298 	mpint	*q;	// group order, q divides p-1
299 	mpint	*alpha;	// group generator
300 	mpint	*key;	// (encryption key) alpha**secret mod p
301 };
302 
303 // private/decryption key
304 struct DSApriv
305 {
306 	DSApub	pub;
307 	mpint	*secret; // (decryption key)
308 };
309 
310 // signature
311 struct DSAsig
312 {
313 	mpint	*r, *s;
314 };
315 
316 DSApriv*	dsagen(DSApub *opub);	// opub not checked for consistency!
317 DSAsig*		dsasign(DSApriv *k, mpint *m);
318 int		dsaverify(DSApub *k, DSAsig *sig, mpint *m);
319 DSApub*		dsapuballoc(void);
320 void		dsapubfree(DSApub*);
321 DSApriv*	dsaprivalloc(void);
322 void		dsaprivfree(DSApriv*);
323 DSAsig*		dsasigalloc(void);
324 void		dsasigfree(DSAsig*);
325 DSApub*		dsaprivtopub(DSApriv*);
326 
327 /////////////////////////////////////////////////////////
328 // TLS
329 /////////////////////////////////////////////////////////
330 typedef struct Thumbprint{
331 	struct Thumbprint *next;
332 	uchar sha1[SHA1dlen];
333 } Thumbprint;
334 
335 typedef struct TLSconn{
336 	char dir[40];  // connection directory
337 	uchar *cert;   // certificate (local on input, remote on output)
338 	uchar *sessionID;
339 	int certlen, sessionIDlen;
340 	int (*trace)(char*fmt, ...);
341 	PEMChain *chain; // optional extra certificate evidence for servers to present
342 } TLSconn;
343 
344 // tlshand.c
345 int tlsClient(int fd, TLSconn *c);
346 int tlsServer(int fd, TLSconn *c);
347 
348 // thumb.c
349 Thumbprint* initThumbprints(char *ok, char *crl);
350 void	freeThumbprints(Thumbprint *ok);
351 int		okThumbprint(uchar *sha1, Thumbprint *ok);
352 
353 // readcert.c
354 uchar	*readcert(char *filename, int *pcertlen);
355 PEMChain *readcertchain(char *filename);
356