Blob


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