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