1
0

libsec.h 10.0 KB

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