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