xref: /plan9-contrib/sys/include/libsec.h (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
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 // DES definitions
10 /////////////////////////////////////////////////////////
11 
12 enum
13 {
14 	DESbsize=	8,
15 };
16 
17 // single des
18 typedef struct DESstate DESstate;
19 struct DESstate
20 {
21 	ulong	setup;
22 	uchar	key[8];		/* unexpanded key */
23 	ulong	expanded[32];	/* expanded key */
24 	uchar	ivec[8];	/* initialization vector */
25 };
26 
27 void	setupDESstate(DESstate *s, uchar key[8], uchar *ivec);
28 void	des_key_setup(uchar[8], ulong[32]);
29 void	block_cipher(ulong*, uchar*, int);
30 void	desCBCencrypt(uchar*, int, DESstate*);
31 void	desCBCdecrypt(uchar*, int, DESstate*);
32 void	desECBencrypt(uchar*, int, DESstate*);
33 void	desECBdecrypt(uchar*, int, DESstate*);
34 
35 // for backward compatibility with 7 byte DES key format
36 void	des56to64(uchar *k56, uchar *k64);
37 void	des64to56(uchar *k64, uchar *k56);
38 void	key_setup(uchar[7], ulong[32]);
39 
40 // triple des encrypt/decrypt orderings
41 enum {
42 	DES3E=		0,
43 	DES3D=		1,
44 	DES3EEE=	0,
45 	DES3EDE=	2,
46 	DES3DED=	5,
47 	DES3DDD=	7,
48 };
49 
50 typedef struct DES3state DES3state;
51 struct DES3state
52 {
53 	ulong	setup;
54 	uchar	key[3][8];		/* unexpanded key */
55 	ulong	expanded[3][32];	/* expanded key */
56 	uchar	ivec[8];		/* initialization vector */
57 };
58 
59 void	setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec);
60 void	triple_block_cipher(ulong keys[3][32], uchar*, int);
61 void	des3CBCencrypt(uchar*, int, DES3state*);
62 void	des3CBCdecrypt(uchar*, int, DES3state*);
63 void	des3ECBencrypt(uchar*, int, DES3state*);
64 void	des3ECBdecrypt(uchar*, int, DES3state*);
65 
66 /////////////////////////////////////////////////////////
67 // digests
68 /////////////////////////////////////////////////////////
69 
70 enum
71 {
72 	SHA1dlen=	20,	/* SHA digest length */
73 	MD4dlen=	16,	/* MD4 digest length */
74 	MD5dlen=	16	/* MD5 digest length */
75 };
76 
77 typedef struct DigestState DigestState;
78 struct DigestState
79 {
80 	ulong len;
81 	u32int state[5];
82 	uchar buf[128];
83 	int blen;
84 	char malloced;
85 	char seeded;
86 };
87 typedef struct DigestState SHAstate;
88 typedef struct DigestState MD5state;
89 typedef struct DigestState MD4state;
90 
91 DigestState* md4(uchar*, ulong, uchar*, DigestState*);
92 DigestState* md5(uchar*, ulong, uchar*, DigestState*);
93 DigestState* sha1(uchar*, ulong, uchar*, DigestState*);
94 DigestState* hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
95 DigestState* hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
96 
97 /////////////////////////////////////////////////////////
98 // base 64 & 32 conversions
99 /////////////////////////////////////////////////////////
100 
101 int	dec64(uchar *out, int lim, char *in, int n);
102 int	enc64(char *out, int lim, uchar *in, int n);
103 int	dec32(uchar *out, int lim, char *in, int n);
104 int	enc32(char *out, int lim, uchar *in, int n);
105 
106 /////////////////////////////////////////////////////////
107 // random number generation
108 /////////////////////////////////////////////////////////
109 void	genrandom(uchar *buf, int nbytes);
110 void	prng(uchar *buf, int nbytes);
111 
112 /////////////////////////////////////////////////////////
113 // primes
114 /////////////////////////////////////////////////////////
115 void	genprime(mpint *p, int n, int accuracy); // generate an n bit probable prime
116 void	gensafeprime(mpint *p, mpint *alpha, int n, int accuracy);	// prime and generator
117 void	genstrongprime(mpint *p, int n, int accuracy);	// generate an n bit strong prime
118 void	DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
119 int	probably_prime(mpint *n, int nrep);	// miller-rabin test
120 int	smallprimetest(mpint *p);		// returns -1 if not prime, 0 otherwise
121 
122 /////////////////////////////////////////////////////////
123 // rc4
124 /////////////////////////////////////////////////////////
125 typedef struct RC4state RC4state;
126 struct RC4state
127 {
128 	 uchar state[256];
129 	 uchar x;
130 	 uchar y;
131 };
132 
133 void	setupRC4state(RC4state*, uchar*, int);
134 void	rc4(RC4state*, uchar*, int);
135 void	rc4skip(RC4state*, int);
136 void	rc4back(RC4state*, int);
137 
138 /////////////////////////////////////////////////////////
139 // rsa
140 /////////////////////////////////////////////////////////
141 typedef struct RSApub RSApub;
142 typedef struct RSApriv RSApriv;
143 
144 // public/encryption key
145 struct RSApub
146 {
147 	mpint	*n;	// modulus
148 	mpint	*ek;	// exp (encryption key)
149 };
150 
151 // private/decryption key
152 struct RSApriv
153 {
154 	RSApub	pub;
155 
156 	mpint	*dk;	// exp (decryption key)
157 
158 	// precomputed values to help with chinese remainder theorem calc
159 	mpint	*p;
160 	mpint	*q;
161 	mpint	*kp;	// dk mod p-1
162 	mpint	*kq;	// dk mod q-1
163 	mpint	*c2;	// for converting modular rep to answer
164 };
165 
166 RSApriv*	rsagen(int nlen, int elen, int rounds);
167 mpint*		rsaencrypt(RSApub *k, mpint *in, mpint *out);
168 mpint*		rsadecrypt(RSApriv *k, mpint *in, mpint *out);
169 RSApub*		rsapuballoc(void);
170 void		rsapubfree(RSApub*);
171 RSApriv*	rsaprivalloc(void);
172 void		rsaprivfree(RSApriv*);
173 RSApub*		rsaprivtopub(RSApriv*);
174 
175 /////////////////////////////////////////////////////////
176 // eg
177 /////////////////////////////////////////////////////////
178 typedef struct EGpub EGpub;
179 typedef struct EGpriv EGpriv;
180 typedef struct EGsig EGsig;
181 
182 // public/encryption key
183 struct EGpub
184 {
185 	mpint	*p;	// modulus
186 	mpint	*alpha;	// generator
187 	mpint	*key;	// (encryption key) alpha**secret mod p
188 };
189 
190 // private/decryption key
191 struct EGpriv
192 {
193 	EGpub	pub;
194 	mpint	*secret; // (decryption key)
195 };
196 
197 // signature
198 struct EGsig
199 {
200 	mpint	*r, *s;
201 };
202 
203 EGpriv*		eggen(int nlen, int rounds);
204 mpint*		egencrypt(EGpub *k, mpint *in, mpint *out);
205 mpint*		egdecrypt(EGpriv *k, mpint *in, mpint *out);
206 EGsig*		egsign(EGpriv *k, mpint *m);
207 int		egverify(EGpub *k, EGsig *sig, mpint *m);
208 EGpub*		egpuballoc(void);
209 void		egpubfree(EGpub*);
210 EGpriv*		egprivalloc(void);
211 void		egprivfree(EGpriv*);
212 EGsig*		egsigalloc(void);
213 void		egsigfree(EGsig*);
214 EGpub*		egprivtopub(EGpriv*);
215