libsec.h 8.6 KB

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