libsec.h 9.9 KB

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