libsec.h 11 KB

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