Blob


1 /*
2 #pragma lib "libsec.a"
3 #pragma src "/sys/src/libsec"
4 */
6 #ifndef _MPINT
7 typedef struct mpint mpint;
8 #endif
10 /////////////////////////////////////////////////////////
11 // AES definitions
12 /////////////////////////////////////////////////////////
14 enum
15 {
16 AESbsize= 16,
17 AESmaxkey= 32,
18 AESmaxrounds= 14
19 };
21 typedef struct AESstate AESstate;
22 struct AESstate
23 {
24 ulong setup;
25 int rounds;
26 int keybytes;
27 uchar key[AESmaxkey]; /* unexpanded key */
28 u32int ekey[4*(AESmaxrounds + 1)]; /* encryption key */
29 u32int dkey[4*(AESmaxrounds + 1)]; /* decryption key */
30 uchar ivec[AESbsize]; /* initialization vector */
31 };
33 void setupAESstate(AESstate *s, uchar key[], int keybytes, uchar *ivec);
34 void aesCBCencrypt(uchar *p, int len, AESstate *s);
35 void aesCBCdecrypt(uchar *p, int len, AESstate *s);
37 /////////////////////////////////////////////////////////
38 // Blowfish Definitions
39 /////////////////////////////////////////////////////////
41 enum
42 {
43 BFbsize = 8,
44 BFrounds = 16
45 };
47 // 16-round Blowfish
48 typedef struct BFstate BFstate;
49 struct BFstate
50 {
51 ulong setup;
53 uchar key[56];
54 uchar ivec[8];
56 u32int pbox[BFrounds+2];
57 u32int sbox[1024];
58 };
60 void setupBFstate(BFstate *s, uchar key[], int keybytes, uchar *ivec);
61 void bfCBCencrypt(uchar*, int, BFstate*);
62 void bfCBCdecrypt(uchar*, int, BFstate*);
63 void bfECBencrypt(uchar*, int, BFstate*);
64 void bfECBdecrypt(uchar*, int, BFstate*);
66 /////////////////////////////////////////////////////////
67 // DES definitions
68 /////////////////////////////////////////////////////////
70 enum
71 {
72 DESbsize= 8
73 };
75 // single des
76 typedef struct DESstate DESstate;
77 struct DESstate
78 {
79 ulong setup;
80 uchar key[8]; /* unexpanded key */
81 ulong expanded[32]; /* expanded key */
82 uchar ivec[8]; /* initialization vector */
83 };
85 void setupDESstate(DESstate *s, uchar key[8], uchar *ivec);
86 void des_key_setup(uchar[8], ulong[32]);
87 void block_cipher(ulong*, uchar*, int);
88 void desCBCencrypt(uchar*, int, DESstate*);
89 void desCBCdecrypt(uchar*, int, DESstate*);
90 void desECBencrypt(uchar*, int, DESstate*);
91 void desECBdecrypt(uchar*, int, DESstate*);
93 // for backward compatibility with 7 byte DES key format
94 void des56to64(uchar *k56, uchar *k64);
95 void des64to56(uchar *k64, uchar *k56);
96 void key_setup(uchar[7], ulong[32]);
98 // triple des encrypt/decrypt orderings
99 enum {
100 DES3E= 0,
101 DES3D= 1,
102 DES3EEE= 0,
103 DES3EDE= 2,
104 DES3DED= 5,
105 DES3DDD= 7
106 };
108 typedef struct DES3state DES3state;
109 struct DES3state
111 ulong setup;
112 uchar key[3][8]; /* unexpanded key */
113 ulong expanded[3][32]; /* expanded key */
114 uchar ivec[8]; /* initialization vector */
115 };
117 void setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec);
118 void triple_block_cipher(ulong keys[3][32], uchar*, int);
119 void des3CBCencrypt(uchar*, int, DES3state*);
120 void des3CBCdecrypt(uchar*, int, DES3state*);
121 void des3ECBencrypt(uchar*, int, DES3state*);
122 void des3ECBdecrypt(uchar*, int, DES3state*);
124 /////////////////////////////////////////////////////////
125 // digests
126 /////////////////////////////////////////////////////////
128 enum
130 SHA1dlen= 20, /* SHA digest length */
131 MD4dlen= 16, /* MD4 digest length */
132 MD5dlen= 16 /* MD5 digest length */
133 };
135 typedef struct DigestState DigestState;
136 struct DigestState
138 ulong len;
139 u32int state[5];
140 uchar buf[128];
141 int blen;
142 char malloced;
143 char seeded;
144 };
145 typedef struct DigestState SHAstate; /* obsolete name */
146 typedef struct DigestState SHA1state;
147 typedef struct DigestState MD5state;
148 typedef struct DigestState MD4state;
150 DigestState* md4(uchar*, ulong, uchar*, DigestState*);
151 DigestState* md5(uchar*, ulong, uchar*, DigestState*);
152 DigestState* sha1(uchar*, ulong, uchar*, DigestState*);
153 DigestState* hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
154 DigestState* hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
155 char* sha1pickle(SHA1state*);
156 SHA1state* sha1unpickle(char*);
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);
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
176 /////////////////////////////////////////////////////////
177 // rc4
178 /////////////////////////////////////////////////////////
179 typedef struct RC4state RC4state;
180 struct RC4state
182 uchar state[256];
183 uchar x;
184 uchar y;
185 };
187 void setupRC4state(RC4state*, uchar*, int);
188 void rc4(RC4state*, uchar*, int);
189 void rc4skip(RC4state*, int);
190 void rc4back(RC4state*, int);
192 /////////////////////////////////////////////////////////
193 // rsa
194 /////////////////////////////////////////////////////////
195 typedef struct RSApub RSApub;
196 typedef struct RSApriv RSApriv;
198 // public/encryption key
199 struct RSApub
201 mpint *n; // modulus
202 mpint *ek; // exp (encryption key)
203 };
205 // private/decryption key
206 struct RSApriv
208 RSApub pub;
210 mpint *dk; // exp (decryption key)
212 // precomputed values to help with chinese remainder theorem calc
213 mpint *p;
214 mpint *q;
215 mpint *kp; // dk mod p-1
216 mpint *kq; // dk mod q-1
217 mpint *c2; // (inv p) mod q
218 };
220 RSApriv* rsagen(int nlen, int elen, int rounds);
221 mpint* rsaencrypt(RSApub *k, mpint *in, mpint *out);
222 mpint* rsadecrypt(RSApriv *k, mpint *in, mpint *out);
223 RSApub* rsapuballoc(void);
224 void rsapubfree(RSApub*);
225 RSApriv* rsaprivalloc(void);
226 void rsaprivfree(RSApriv*);
227 RSApub* rsaprivtopub(RSApriv*);
228 RSApub* X509toRSApub(uchar*, int, char*, int);
229 RSApriv* asn1toRSApriv(uchar*, int);
230 uchar* decodepem(char *s, char *type, int *len);
231 uchar* X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
233 /////////////////////////////////////////////////////////
234 // elgamal
235 /////////////////////////////////////////////////////////
236 typedef struct EGpub EGpub;
237 typedef struct EGpriv EGpriv;
238 typedef struct EGsig EGsig;
240 // public/encryption key
241 struct EGpub
243 mpint *p; // modulus
244 mpint *alpha; // generator
245 mpint *key; // (encryption key) alpha**secret mod p
246 };
248 // private/decryption key
249 struct EGpriv
251 EGpub pub;
252 mpint *secret; // (decryption key)
253 };
255 // signature
256 struct EGsig
258 mpint *r, *s;
259 };
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*);
274 /////////////////////////////////////////////////////////
275 // dsa
276 /////////////////////////////////////////////////////////
277 typedef struct DSApub DSApub;
278 typedef struct DSApriv DSApriv;
279 typedef struct DSAsig DSAsig;
281 // public/encryption key
282 struct DSApub
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 };
290 // private/decryption key
291 struct DSApriv
293 DSApub pub;
294 mpint *secret; // (decryption key)
295 };
297 // signature
298 struct DSAsig
300 mpint *r, *s;
301 };
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*);
314 /////////////////////////////////////////////////////////
315 // TLS
316 /////////////////////////////////////////////////////////
317 typedef struct Thumbprint{
318 struct Thumbprint *next;
319 uchar sha1[SHA1dlen];
320 } Thumbprint;
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;
330 // tlshand.c
331 extern int tlsClient(int fd, TLSconn *c);
332 extern int tlsServer(int fd, TLSconn *c);
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);
339 // readcert.c
340 extern uchar *readcert(char *filename, int *pcertlen);