libsec.h 8.5 KB

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