libsec.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. };
  111. typedef struct DigestState DigestState;
  112. struct DigestState
  113. {
  114. uvlong len;
  115. u32int state[5];
  116. uchar buf[128];
  117. int blen;
  118. char malloced;
  119. char seeded;
  120. };
  121. typedef struct DigestState SHAstate; /* obsolete name */
  122. typedef struct DigestState SHA1state;
  123. typedef struct DigestState MD5state;
  124. typedef struct DigestState MD4state;
  125. DigestState* md4(uchar*, ulong, uchar*, DigestState*);
  126. DigestState* md5(uchar*, ulong, uchar*, DigestState*);
  127. DigestState* sha1(uchar*, ulong, uchar*, DigestState*);
  128. DigestState* hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
  129. DigestState* hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
  130. char* md5pickle(MD5state*);
  131. MD5state* md5unpickle(char*);
  132. char* sha1pickle(SHA1state*);
  133. SHA1state* sha1unpickle(char*);
  134. /////////////////////////////////////////////////////////
  135. // random number generation
  136. /////////////////////////////////////////////////////////
  137. void genrandom(uchar *buf, int nbytes);
  138. void prng(uchar *buf, int nbytes);
  139. ulong fastrand(void);
  140. ulong nfastrand(ulong);
  141. /////////////////////////////////////////////////////////
  142. // primes
  143. /////////////////////////////////////////////////////////
  144. void genprime(mpint *p, int n, int accuracy); // generate an n bit probable prime
  145. void gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); // prime and generator
  146. void genstrongprime(mpint *p, int n, int accuracy); // generate an n bit strong prime
  147. void DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
  148. int probably_prime(mpint *n, int nrep); // miller-rabin test
  149. int smallprimetest(mpint *p); // returns -1 if not prime, 0 otherwise
  150. /////////////////////////////////////////////////////////
  151. // rc4
  152. /////////////////////////////////////////////////////////
  153. typedef struct RC4state RC4state;
  154. struct RC4state
  155. {
  156. uchar state[256];
  157. uchar x;
  158. uchar y;
  159. };
  160. void setupRC4state(RC4state*, uchar*, int);
  161. void rc4(RC4state*, uchar*, int);
  162. void rc4skip(RC4state*, int);
  163. void rc4back(RC4state*, int);
  164. /////////////////////////////////////////////////////////
  165. // rsa
  166. /////////////////////////////////////////////////////////
  167. typedef struct RSApub RSApub;
  168. typedef struct RSApriv RSApriv;
  169. typedef struct PEMChain PEMChain;
  170. // public/encryption key
  171. struct RSApub
  172. {
  173. mpint *n; // modulus
  174. mpint *ek; // exp (encryption key)
  175. };
  176. // private/decryption key
  177. struct RSApriv
  178. {
  179. RSApub pub;
  180. mpint *dk; // exp (decryption key)
  181. // precomputed values to help with chinese remainder theorem calc
  182. mpint *p;
  183. mpint *q;
  184. mpint *kp; // dk mod p-1
  185. mpint *kq; // dk mod q-1
  186. mpint *c2; // (inv p) mod q
  187. };
  188. struct PEMChain{
  189. PEMChain *next;
  190. uchar *pem;
  191. int pemlen;
  192. };
  193. RSApriv* rsagen(int nlen, int elen, int rounds);
  194. RSApriv* rsafill(mpint *n, mpint *e, mpint *d, mpint *p, mpint *q);
  195. mpint* rsaencrypt(RSApub *k, mpint *in, mpint *out);
  196. mpint* rsadecrypt(RSApriv *k, mpint *in, mpint *out);
  197. RSApub* rsapuballoc(void);
  198. void rsapubfree(RSApub*);
  199. RSApriv* rsaprivalloc(void);
  200. void rsaprivfree(RSApriv*);
  201. RSApub* rsaprivtopub(RSApriv*);
  202. RSApub* X509toRSApub(uchar*, int, char*, int);
  203. RSApriv* asn1toRSApriv(uchar*, int);
  204. void asn1dump(uchar *der, int len);
  205. uchar* decodePEM(char *s, char *type, int *len, char **new_s);
  206. PEMChain* decodepemchain(char *s, char *type);
  207. uchar* X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
  208. uchar* X509req(RSApriv *priv, char *subj, int *certlen);
  209. char* X509verify(uchar *cert, int ncert, RSApub *pk);
  210. void X509dump(uchar *cert, int ncert);
  211. /////////////////////////////////////////////////////////
  212. // elgamal
  213. /////////////////////////////////////////////////////////
  214. typedef struct EGpub EGpub;
  215. typedef struct EGpriv EGpriv;
  216. typedef struct EGsig EGsig;
  217. // public/encryption key
  218. struct EGpub
  219. {
  220. mpint *p; // modulus
  221. mpint *alpha; // generator
  222. mpint *key; // (encryption key) alpha**secret mod p
  223. };
  224. // private/decryption key
  225. struct EGpriv
  226. {
  227. EGpub pub;
  228. mpint *secret; // (decryption key)
  229. };
  230. // signature
  231. struct EGsig
  232. {
  233. mpint *r, *s;
  234. };
  235. EGpriv* eggen(int nlen, int rounds);
  236. mpint* egencrypt(EGpub *k, mpint *in, mpint *out); // deprecated
  237. mpint* egdecrypt(EGpriv *k, mpint *in, mpint *out);
  238. EGsig* egsign(EGpriv *k, mpint *m);
  239. int egverify(EGpub *k, EGsig *sig, mpint *m);
  240. EGpub* egpuballoc(void);
  241. void egpubfree(EGpub*);
  242. EGpriv* egprivalloc(void);
  243. void egprivfree(EGpriv*);
  244. EGsig* egsigalloc(void);
  245. void egsigfree(EGsig*);
  246. EGpub* egprivtopub(EGpriv*);
  247. /////////////////////////////////////////////////////////
  248. // dsa
  249. /////////////////////////////////////////////////////////
  250. typedef struct DSApub DSApub;
  251. typedef struct DSApriv DSApriv;
  252. typedef struct DSAsig DSAsig;
  253. // public/encryption key
  254. struct DSApub
  255. {
  256. mpint *p; // modulus
  257. mpint *q; // group order, q divides p-1
  258. mpint *alpha; // group generator
  259. mpint *key; // (encryption key) alpha**secret mod p
  260. };
  261. // private/decryption key
  262. struct DSApriv
  263. {
  264. DSApub pub;
  265. mpint *secret; // (decryption key)
  266. };
  267. // signature
  268. struct DSAsig
  269. {
  270. mpint *r, *s;
  271. };
  272. DSApriv* dsagen(DSApub *opub); // opub not checked for consistency!
  273. DSAsig* dsasign(DSApriv *k, mpint *m);
  274. int dsaverify(DSApub *k, DSAsig *sig, mpint *m);
  275. DSApub* dsapuballoc(void);
  276. void dsapubfree(DSApub*);
  277. DSApriv* dsaprivalloc(void);
  278. void dsaprivfree(DSApriv*);
  279. DSAsig* dsasigalloc(void);
  280. void dsasigfree(DSAsig*);
  281. DSApub* dsaprivtopub(DSApriv*);
  282. /////////////////////////////////////////////////////////
  283. // TLS
  284. /////////////////////////////////////////////////////////
  285. typedef struct Thumbprint{
  286. struct Thumbprint *next;
  287. uchar sha1[SHA1dlen];
  288. } Thumbprint;
  289. typedef struct TLSconn{
  290. char dir[40]; // connection directory
  291. uchar *cert; // certificate (local on input, remote on output)
  292. uchar *sessionID;
  293. int certlen, sessionIDlen;
  294. int (*trace)(char*fmt, ...);
  295. PEMChain *chain; // optional extra certificate evidence for servers to present
  296. char *sessionType;
  297. uchar *sessionKey;
  298. int sessionKeylen;
  299. char *sessionConst;
  300. } TLSconn;
  301. // tlshand.c
  302. int tlsClient(int fd, TLSconn *c);
  303. int tlsServer(int fd, TLSconn *c);
  304. // thumb.c
  305. Thumbprint* initThumbprints(char *ok, char *crl);
  306. void freeThumbprints(Thumbprint *ok);
  307. int okThumbprint(uchar *sha1, Thumbprint *ok);
  308. // readcert.c
  309. uchar *readcert(char *filename, int *pcertlen);
  310. PEMChain *readcertchain(char *filename);