libsec.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #ifndef _MPINT
  2. typedef struct mpint mpint;
  3. #endif
  4. /////////////////////////////////////////////////////////
  5. // AES definitions
  6. /////////////////////////////////////////////////////////
  7. enum
  8. {
  9. AESbsize= 16,
  10. AESmaxkey= 32,
  11. AESmaxrounds= 14
  12. };
  13. typedef struct AESstate AESstate;
  14. struct AESstate
  15. {
  16. ulong setup;
  17. int rounds;
  18. int keybytes;
  19. uchar key[AESmaxkey]; /* unexpanded key */
  20. u32int ekey[4*(AESmaxrounds + 1)]; /* encryption key */
  21. u32int dkey[4*(AESmaxrounds + 1)]; /* decryption key */
  22. uchar ivec[AESbsize]; /* initialization vector */
  23. };
  24. void setupAESstate(AESstate *s, uchar key[], int keybytes, uchar *ivec);
  25. void aesCBCencrypt(uchar *p, int len, AESstate *s);
  26. void aesCBCdecrypt(uchar *p, int len, AESstate *s);
  27. /////////////////////////////////////////////////////////
  28. // Blowfish Definitions
  29. /////////////////////////////////////////////////////////
  30. enum
  31. {
  32. BFbsize = 8,
  33. BFrounds = 16
  34. };
  35. // 16-round Blowfish
  36. typedef struct BFstate BFstate;
  37. struct BFstate
  38. {
  39. ulong setup;
  40. uchar key[56];
  41. uchar ivec[8];
  42. u32int pbox[BFrounds+2];
  43. u32int sbox[1024];
  44. };
  45. void setupBFstate(BFstate *s, uchar key[], int keybytes, uchar *ivec);
  46. void bfCBCencrypt(uchar*, int, BFstate*);
  47. void bfCBCdecrypt(uchar*, int, BFstate*);
  48. void bfECBencrypt(uchar*, int, BFstate*);
  49. void bfECBdecrypt(uchar*, int, BFstate*);
  50. /////////////////////////////////////////////////////////
  51. // DES definitions
  52. /////////////////////////////////////////////////////////
  53. enum
  54. {
  55. DESbsize= 8
  56. };
  57. // single des
  58. typedef struct DESstate DESstate;
  59. struct DESstate
  60. {
  61. ulong setup;
  62. uchar key[8]; /* unexpanded key */
  63. ulong expanded[32]; /* expanded key */
  64. uchar ivec[8]; /* initialization vector */
  65. };
  66. void setupDESstate(DESstate *s, uchar key[8], uchar *ivec);
  67. void des_key_setup(uchar[8], ulong[32]);
  68. void block_cipher(ulong*, uchar*, int);
  69. void desCBCencrypt(uchar*, int, DESstate*);
  70. void desCBCdecrypt(uchar*, int, DESstate*);
  71. void desECBencrypt(uchar*, int, DESstate*);
  72. void desECBdecrypt(uchar*, int, DESstate*);
  73. // for backward compatibility with 7 byte DES key format
  74. void des56to64(uchar *k56, uchar *k64);
  75. void des64to56(uchar *k64, uchar *k56);
  76. void key_setup(uchar[7], ulong[32]);
  77. // triple des encrypt/decrypt orderings
  78. enum {
  79. DES3E= 0,
  80. DES3D= 1,
  81. DES3EEE= 0,
  82. DES3EDE= 2,
  83. DES3DED= 5,
  84. DES3DDD= 7
  85. };
  86. typedef struct DES3state DES3state;
  87. struct DES3state
  88. {
  89. ulong setup;
  90. uchar key[3][8]; /* unexpanded key */
  91. ulong expanded[3][32]; /* expanded key */
  92. uchar ivec[8]; /* initialization vector */
  93. };
  94. void setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec);
  95. void triple_block_cipher(ulong keys[3][32], uchar*, int);
  96. void des3CBCencrypt(uchar*, int, DES3state*);
  97. void des3CBCdecrypt(uchar*, int, DES3state*);
  98. void des3ECBencrypt(uchar*, int, DES3state*);
  99. void des3ECBdecrypt(uchar*, int, DES3state*);
  100. /////////////////////////////////////////////////////////
  101. // digests
  102. /////////////////////////////////////////////////////////
  103. enum
  104. {
  105. SHA1dlen= 20, /* SHA digest length */
  106. MD4dlen= 16, /* MD4 digest length */
  107. MD5dlen= 16 /* MD5 digest length */
  108. };
  109. typedef struct DigestState DigestState;
  110. struct DigestState
  111. {
  112. ulong len;
  113. u32int state[5];
  114. uchar buf[128];
  115. int blen;
  116. char malloced;
  117. char seeded;
  118. };
  119. typedef struct DigestState SHAstate; /* obsolete name */
  120. typedef struct DigestState SHA1state;
  121. typedef struct DigestState MD5state;
  122. typedef struct DigestState MD4state;
  123. DigestState* md4(uchar*, ulong, uchar*, DigestState*);
  124. DigestState* md5(uchar*, ulong, uchar*, DigestState*);
  125. DigestState* sha1(uchar*, ulong, uchar*, DigestState*);
  126. DigestState* hmac_md5(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
  127. DigestState* hmac_sha1(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
  128. char* sha1pickle(SHA1state*);
  129. SHA1state* sha1unpickle(char*);
  130. /////////////////////////////////////////////////////////
  131. // random number generation
  132. /////////////////////////////////////////////////////////
  133. void genrandom(uchar *buf, int nbytes);
  134. void prng(uchar *buf, int nbytes);
  135. ulong fastrand(void);
  136. ulong nfastrand(ulong);
  137. /////////////////////////////////////////////////////////
  138. // primes
  139. /////////////////////////////////////////////////////////
  140. void genprime(mpint *p, int n, int accuracy); // generate an n bit probable prime
  141. void gensafeprime(mpint *p, mpint *alpha, int n, int accuracy); // prime and generator
  142. void genstrongprime(mpint *p, int n, int accuracy); // generate an n bit strong prime
  143. void DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen]);
  144. int probably_prime(mpint *n, int nrep); // miller-rabin test
  145. int smallprimetest(mpint *p); // returns -1 if not prime, 0 otherwise
  146. /////////////////////////////////////////////////////////
  147. // rc4
  148. /////////////////////////////////////////////////////////
  149. typedef struct RC4state RC4state;
  150. struct RC4state
  151. {
  152. uchar state[256];
  153. uchar x;
  154. uchar y;
  155. };
  156. void setupRC4state(RC4state*, uchar*, int);
  157. void rc4(RC4state*, uchar*, int);
  158. void rc4skip(RC4state*, int);
  159. void rc4back(RC4state*, int);
  160. /////////////////////////////////////////////////////////
  161. // rsa
  162. /////////////////////////////////////////////////////////
  163. typedef struct RSApub RSApub;
  164. typedef struct RSApriv RSApriv;
  165. // public/encryption key
  166. struct RSApub
  167. {
  168. mpint *n; // modulus
  169. mpint *ek; // exp (encryption key)
  170. };
  171. // private/decryption key
  172. struct RSApriv
  173. {
  174. RSApub pub;
  175. mpint *dk; // exp (decryption key)
  176. // precomputed values to help with chinese remainder theorem calc
  177. mpint *p;
  178. mpint *q;
  179. mpint *kp; // dk mod p-1
  180. mpint *kq; // dk mod q-1
  181. mpint *c2; // (inv p) mod q
  182. };
  183. RSApriv* rsagen(int nlen, int elen, int rounds);
  184. RSApriv* rsafill(mpint *n, mpint *e, mpint *d, mpint *p, mpint *q);
  185. mpint* rsaencrypt(RSApub *k, mpint *in, mpint *out);
  186. mpint* rsadecrypt(RSApriv *k, mpint *in, mpint *out);
  187. RSApub* rsapuballoc(void);
  188. void rsapubfree(RSApub*);
  189. RSApriv* rsaprivalloc(void);
  190. void rsaprivfree(RSApriv*);
  191. RSApub* rsaprivtopub(RSApriv*);
  192. RSApub* X509toRSApub(uchar*, int, char*, int);
  193. RSApriv* asn1toRSApriv(uchar*, int);
  194. void asn1dump(uchar *der, int len);
  195. uchar* decodepem(char *s, char *type, int *len);
  196. uchar* X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
  197. uchar* X509req(RSApriv *priv, char *subj, int *certlen);
  198. char* X509verify(uchar *cert, int ncert, RSApub *pk);
  199. void X509dump(uchar *cert, int ncert);
  200. /////////////////////////////////////////////////////////
  201. // elgamal
  202. /////////////////////////////////////////////////////////
  203. typedef struct EGpub EGpub;
  204. typedef struct EGpriv EGpriv;
  205. typedef struct EGsig EGsig;
  206. // public/encryption key
  207. struct EGpub
  208. {
  209. mpint *p; // modulus
  210. mpint *alpha; // generator
  211. mpint *key; // (encryption key) alpha**secret mod p
  212. };
  213. // private/decryption key
  214. struct EGpriv
  215. {
  216. EGpub pub;
  217. mpint *secret; // (decryption key)
  218. };
  219. // signature
  220. struct EGsig
  221. {
  222. mpint *r, *s;
  223. };
  224. EGpriv* eggen(int nlen, int rounds);
  225. mpint* egencrypt(EGpub *k, mpint *in, mpint *out);
  226. mpint* egdecrypt(EGpriv *k, mpint *in, mpint *out);
  227. EGsig* egsign(EGpriv *k, mpint *m);
  228. int egverify(EGpub *k, EGsig *sig, mpint *m);
  229. EGpub* egpuballoc(void);
  230. void egpubfree(EGpub*);
  231. EGpriv* egprivalloc(void);
  232. void egprivfree(EGpriv*);
  233. EGsig* egsigalloc(void);
  234. void egsigfree(EGsig*);
  235. EGpub* egprivtopub(EGpriv*);
  236. /////////////////////////////////////////////////////////
  237. // dsa
  238. /////////////////////////////////////////////////////////
  239. typedef struct DSApub DSApub;
  240. typedef struct DSApriv DSApriv;
  241. typedef struct DSAsig DSAsig;
  242. // public/encryption key
  243. struct DSApub
  244. {
  245. mpint *p; // modulus
  246. mpint *q; // group order, q divides p-1
  247. mpint *alpha; // group generator
  248. mpint *key; // (encryption key) alpha**secret mod p
  249. };
  250. // private/decryption key
  251. struct DSApriv
  252. {
  253. DSApub pub;
  254. mpint *secret; // (decryption key)
  255. };
  256. // signature
  257. struct DSAsig
  258. {
  259. mpint *r, *s;
  260. };
  261. DSApriv* dsagen(DSApub *opub);
  262. DSAsig* dsasign(DSApriv *k, mpint *m);
  263. int dsaverify(DSApub *k, DSAsig *sig, mpint *m);
  264. DSApub* dsapuballoc(void);
  265. void dsapubfree(DSApub*);
  266. DSApriv* dsaprivalloc(void);
  267. void dsaprivfree(DSApriv*);
  268. DSAsig* dsasigalloc(void);
  269. void dsasigfree(DSAsig*);
  270. DSApub* dsaprivtopub(DSApriv*);
  271. /////////////////////////////////////////////////////////
  272. // TLS
  273. /////////////////////////////////////////////////////////
  274. typedef struct Thumbprint{
  275. struct Thumbprint *next;
  276. uchar sha1[SHA1dlen];
  277. } Thumbprint;
  278. typedef struct TLSconn{
  279. char dir[40]; // connection directory
  280. uchar *cert; // certificate (local on input, remote on output)
  281. uchar *sessionID;
  282. int certlen, sessionIDlen;
  283. int (*trace)(char*fmt, ...);
  284. } TLSconn;
  285. // tlshand.c
  286. extern int tlsClient(int fd, TLSconn *c);
  287. extern int tlsServer(int fd, TLSconn *c);
  288. // thumb.c
  289. extern Thumbprint* initThumbprints(char *ok, char *crl);
  290. extern void freeThumbprints(Thumbprint *ok);
  291. extern int okThumbprint(uchar *sha1, Thumbprint *ok);
  292. // readcert.c
  293. extern uchar *readcert(char *filename, int *pcertlen);