libsec.h 8.8 KB

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