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