crypto.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include <string.h>
  5. #include <openssl/err.h>
  6. #include <openssl/rsa.h>
  7. #include <openssl/pem.h>
  8. #include <openssl/evp.h>
  9. #include <openssl/rand.h>
  10. #include <openssl/opensslv.h>
  11. #include <openssl/bn.h>
  12. #include <openssl/dh.h>
  13. #include <stdlib.h>
  14. #include <assert.h>
  15. #include <stdio.h>
  16. #include "crypto.h"
  17. #include "../or/or.h"
  18. #include "log.h"
  19. #if OPENSSL_VERSION_NUMBER < 0x00905000l
  20. #error "We require openssl >= 0.9.5"
  21. #elif OPENSSL_VERSION_NUMBER < 0x00906000l
  22. #define OPENSSL_095
  23. #endif
  24. /*
  25. * Certain functions that return a success code in OpenSSL 0.9.6 return void
  26. * (and don't indicate errors) in OpenSSL version 0.9.5.
  27. *
  28. * [OpenSSL 0.9.5 matters, because it ships with Redhat 6.2.]
  29. */
  30. #ifdef OPENSSL_095
  31. #define RETURN_SSL_OUTCOME(exp) (exp); return 0
  32. #else
  33. #define RETURN_SSL_OUTCOME(exp) return !(exp)
  34. #endif
  35. static inline const EVP_CIPHER *
  36. crypto_cipher_evp_cipher(int type, int enc);
  37. static inline int
  38. crypto_cipher_iv_length(int type) {
  39. /*
  40. printf("%d -> %d IV\n",type, EVP_CIPHER_iv_length(
  41. crypto_cipher_evp_cipher(type,0)));
  42. */
  43. switch(type)
  44. {
  45. case CRYPTO_CIPHER_IDENTITY: return 0;
  46. case CRYPTO_CIPHER_DES: return 8;
  47. case CRYPTO_CIPHER_RC4: return 16;
  48. case CRYPTO_CIPHER_3DES: return 8;
  49. default: assert(0); return -1;
  50. }
  51. }
  52. static inline int
  53. crypto_cipher_key_length(int type) {
  54. /*
  55. printf("%d -> %d\n",type, EVP_CIPHER_key_length(
  56. crypto_cipher_evp_cipher(type,0)));
  57. */
  58. switch(type)
  59. {
  60. case CRYPTO_CIPHER_IDENTITY: return 0;
  61. case CRYPTO_CIPHER_DES: return 8;
  62. case CRYPTO_CIPHER_RC4: return 16;
  63. case CRYPTO_CIPHER_3DES: return 16;
  64. default: assert(0); return -1;
  65. }
  66. }
  67. static inline const EVP_CIPHER *
  68. crypto_cipher_evp_cipher(int type, int enc) {
  69. switch(type)
  70. {
  71. case CRYPTO_CIPHER_IDENTITY: return EVP_enc_null();
  72. case CRYPTO_CIPHER_DES: return EVP_des_ofb();
  73. case CRYPTO_CIPHER_RC4: return EVP_rc4();
  74. case CRYPTO_CIPHER_3DES: return EVP_des_ede_ofb();
  75. default: return NULL;
  76. }
  77. }
  78. int crypto_global_init()
  79. {
  80. ERR_load_crypto_strings();
  81. return 0;
  82. }
  83. int crypto_global_cleanup()
  84. {
  85. ERR_free_strings();
  86. return 0;
  87. }
  88. crypto_pk_env_t *crypto_new_pk_env(int type)
  89. {
  90. crypto_pk_env_t *env;
  91. env = (crypto_pk_env_t *)tor_malloc(sizeof(crypto_pk_env_t));
  92. env->type = type;
  93. env->refs = 1;
  94. env->key = NULL;
  95. env->aux = NULL;
  96. switch(type) {
  97. case CRYPTO_PK_RSA:
  98. env->key = (unsigned char *)RSA_new();
  99. if (!env->key) {
  100. free((void *)env);
  101. return NULL;
  102. }
  103. break;
  104. default:
  105. free((void *)env);
  106. return NULL;
  107. break;
  108. }
  109. return env;
  110. }
  111. void crypto_free_pk_env(crypto_pk_env_t *env)
  112. {
  113. assert(env);
  114. if(--env->refs > 0)
  115. return;
  116. switch(env->type) {
  117. case CRYPTO_PK_RSA:
  118. if (env->key)
  119. RSA_free((RSA *)env->key);
  120. break;
  121. default:
  122. break;
  123. }
  124. free((void *)env);
  125. return;
  126. }
  127. /* Create a new crypto_cipher_env_t for a given onion cipher type, key,
  128. * iv, and encryption flag (1=encrypt, 0=decrypt). Return the crypto object
  129. * on success; NULL on failure.
  130. */
  131. crypto_cipher_env_t *
  132. crypto_create_init_cipher(int cipher_type, char *key, char *iv, int encrypt_mode)
  133. {
  134. int r;
  135. crypto_cipher_env_t *crypto = NULL;
  136. if (! (crypto = crypto_new_cipher_env(cipher_type))) {
  137. log(LOG_ERR, "Unable to allocate crypto object");
  138. return NULL;
  139. }
  140. if (crypto_cipher_set_key(crypto, key)) {
  141. log(LOG_ERR, "Unable to set key: %s", crypto_perror());
  142. goto error;
  143. }
  144. if (crypto_cipher_set_iv(crypto, iv)) {
  145. log(LOG_ERR, "Unable to set iv: %s", crypto_perror());
  146. goto error;
  147. }
  148. if (encrypt_mode)
  149. r = crypto_cipher_encrypt_init_cipher(crypto);
  150. else
  151. r = crypto_cipher_decrypt_init_cipher(crypto);
  152. if (r) {
  153. log(LOG_ERR, "Unabble to initialize cipher: %s", crypto_perror());
  154. goto error;
  155. }
  156. return crypto;
  157. error:
  158. if (crypto)
  159. crypto_free_cipher_env(crypto);
  160. return NULL;
  161. }
  162. crypto_cipher_env_t *crypto_new_cipher_env(int type)
  163. {
  164. crypto_cipher_env_t *env;
  165. int iv_len, key_len;
  166. env = (crypto_cipher_env_t *)tor_malloc(sizeof(crypto_cipher_env_t));
  167. env->type = type;
  168. env->key = NULL;
  169. env->iv = NULL;
  170. env->aux = NULL;
  171. iv_len = crypto_cipher_iv_length(type);
  172. key_len = crypto_cipher_key_length(type);
  173. if (! crypto_cipher_evp_cipher(type,0))
  174. /* This is not an openssl cipher */
  175. goto err;
  176. else {
  177. env->aux = (unsigned char *)tor_malloc(sizeof(EVP_CIPHER_CTX));
  178. EVP_CIPHER_CTX_init((EVP_CIPHER_CTX *)env->aux);
  179. }
  180. if(iv_len)
  181. env->iv = (unsigned char *)tor_malloc(iv_len);
  182. if(key_len)
  183. env->key = (unsigned char *)tor_malloc(key_len);
  184. return env;
  185. err:
  186. if (env->key)
  187. free(env->key);
  188. if (env->iv)
  189. free(env->iv);
  190. if (env->aux)
  191. free(env->aux);
  192. if (env)
  193. free(env);
  194. return NULL;
  195. }
  196. void crypto_free_cipher_env(crypto_cipher_env_t *env)
  197. {
  198. assert(env);
  199. if (crypto_cipher_evp_cipher(env->type,0)) {
  200. /* This is an openssl cipher */
  201. assert(env->aux);
  202. EVP_CIPHER_CTX_cleanup((EVP_CIPHER_CTX *)env->aux);
  203. }
  204. if (env->aux)
  205. free((void *)env->aux);
  206. if (env->iv)
  207. free((void *)env->iv);
  208. if (env->key)
  209. free((void *)env->key);
  210. free((void *)env);
  211. }
  212. /* public key crypto */
  213. int crypto_pk_generate_key(crypto_pk_env_t *env)
  214. {
  215. assert(env);
  216. switch(env->type) {
  217. case CRYPTO_PK_RSA:
  218. if (env->key)
  219. RSA_free((RSA *)env->key);
  220. env->key = (unsigned char *)RSA_generate_key(1024,65537, NULL, NULL);
  221. if (!env->key)
  222. return -1;
  223. break;
  224. default:
  225. return -1;
  226. }
  227. return 0;
  228. }
  229. int crypto_pk_read_private_key_from_file(crypto_pk_env_t *env, FILE *src)
  230. {
  231. assert(env && src);
  232. switch(env->type) {
  233. case CRYPTO_PK_RSA:
  234. if (env->key)
  235. RSA_free((RSA *)env->key);
  236. env->key = (unsigned char *)PEM_read_RSAPrivateKey(src, NULL, NULL, NULL);
  237. if (!env->key)
  238. return -1;
  239. break;
  240. default :
  241. return -1;
  242. }
  243. return 0;
  244. }
  245. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env, unsigned char *keyfile)
  246. {
  247. FILE *f_pr;
  248. int retval = 0;
  249. assert(env && keyfile);
  250. if (strspn(keyfile,CONFIG_LEGAL_FILENAME_CHARACTERS) == strlen(keyfile)) /* filename contains legal characters only */
  251. {
  252. /* open the keyfile */
  253. f_pr=fopen(keyfile,"rb");
  254. if (!f_pr)
  255. return -1;
  256. /* read the private key */
  257. retval = crypto_pk_read_private_key_from_file(env, f_pr);
  258. fclose(f_pr);
  259. if (retval == -1)
  260. {
  261. log(LOG_ERR,"Error reading private key : %s",crypto_perror());
  262. return -1;
  263. }
  264. /* check the private key */
  265. retval = crypto_pk_check_key(env);
  266. if (retval == 0)
  267. {
  268. log(LOG_ERR,"Private key read but is invalid : %s.", crypto_perror());
  269. return -1;
  270. }
  271. else if (retval == -1)
  272. {
  273. log(LOG_ERR,"Private key read but validity checking failed : %s",crypto_perror());
  274. return -1;
  275. }
  276. else if (retval == 1)
  277. {
  278. return 0;
  279. }
  280. } /* filename contains legal characters only */
  281. return -1; /* report error */
  282. }
  283. int crypto_pk_read_public_key_from_file(crypto_pk_env_t *env, FILE *src)
  284. {
  285. assert(env && src);
  286. switch(env->type) {
  287. case CRYPTO_PK_RSA:
  288. if(env->key)
  289. RSA_free((RSA *)env->key);
  290. env->key = (unsigned char *)PEM_read_RSAPublicKey(src, NULL, NULL, NULL);
  291. if (!env->key)
  292. return -1;
  293. break;
  294. default :
  295. return -1;
  296. }
  297. return 0;
  298. }
  299. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, int *len) {
  300. BUF_MEM *buf;
  301. BIO *b;
  302. assert(env && env->key && dest);
  303. switch(env->type) {
  304. case CRYPTO_PK_RSA:
  305. b = BIO_new(BIO_s_mem()); /* Create a memory BIO */
  306. /* Now you can treat b as if it were a file. Just use the
  307. * * PEM_*_bio_* functions instead of the non-bio variants.
  308. * */
  309. if(!PEM_write_bio_RSAPublicKey(b, (RSA *)env->key))
  310. return -1;
  311. BIO_get_mem_ptr(b, &buf);
  312. BIO_set_close(b, BIO_NOCLOSE); /* so BIO_free doesn't free buf */
  313. BIO_free(b);
  314. *dest = tor_malloc(buf->length+1);
  315. memcpy(*dest, buf->data, buf->length);
  316. (*dest)[buf->length] = 0; /* null terminate it */
  317. *len = buf->length;
  318. BUF_MEM_free(buf);
  319. break;
  320. default:
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, char *src, int len) {
  326. BIO *b;
  327. assert(env && src);
  328. switch(env->type) {
  329. case CRYPTO_PK_RSA:
  330. b = BIO_new(BIO_s_mem()); /* Create a memory BIO */
  331. BIO_write(b, src, len);
  332. RSA_free((RSA *)env->key);
  333. env->key = (unsigned char *)PEM_read_bio_RSAPublicKey(b, NULL, NULL, NULL);
  334. if(!env->key)
  335. return -1;
  336. BIO_free(b);
  337. break;
  338. default:
  339. return -1;
  340. }
  341. return 0;
  342. }
  343. int crypto_pk_write_private_key_to_file(crypto_pk_env_t *env, FILE *dest)
  344. {
  345. assert(env && dest);
  346. switch(env->type) {
  347. case CRYPTO_PK_RSA:
  348. if (!env->key)
  349. return -1;
  350. if (PEM_write_RSAPrivateKey(dest, (RSA *)env->key, NULL, NULL, 0,0, NULL) == 0)
  351. return -1;
  352. break;
  353. default :
  354. return -1;
  355. }
  356. return 0;
  357. }
  358. int crypto_pk_write_public_key_to_file(crypto_pk_env_t *env, FILE *dest)
  359. {
  360. assert(env && dest);
  361. switch(env->type) {
  362. case CRYPTO_PK_RSA:
  363. if (!env->key)
  364. return -1;
  365. if (PEM_write_RSAPublicKey(dest, (RSA *)env->key) == 0)
  366. return -1;
  367. break;
  368. default :
  369. return -1;
  370. }
  371. return 0;
  372. }
  373. int crypto_pk_check_key(crypto_pk_env_t *env)
  374. {
  375. assert(env);
  376. switch(env->type) {
  377. case CRYPTO_PK_RSA:
  378. return RSA_check_key((RSA *)env->key);
  379. default:
  380. return -1;
  381. }
  382. }
  383. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b) {
  384. int result;
  385. if (!a || !b)
  386. return -1;
  387. if (!a->key || !b->key)
  388. return -1;
  389. if (a->type != b->type)
  390. return -1;
  391. switch(a->type) {
  392. case CRYPTO_PK_RSA:
  393. assert(((RSA *)a->key)->n && ((RSA *)a->key)->e && ((RSA *)b->key)->n && ((RSA *)b->key)->e);
  394. result = BN_cmp(((RSA *)a->key)->n, ((RSA *)b->key)->n);
  395. if (result)
  396. return result;
  397. return BN_cmp(((RSA *)a->key)->e, ((RSA *)b->key)->e);
  398. default:
  399. return -1;
  400. }
  401. }
  402. int crypto_pk_keysize(crypto_pk_env_t *env)
  403. {
  404. assert(env && env->key);
  405. return RSA_size((RSA *)env->key);
  406. }
  407. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *env) {
  408. assert(env && env->key);
  409. switch(env->type) {
  410. case CRYPTO_PK_RSA:
  411. env->refs++;
  412. break;
  413. default:
  414. return NULL;
  415. }
  416. return env;
  417. }
  418. int crypto_pk_public_encrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding)
  419. {
  420. assert(env && from && to);
  421. switch(env->type) {
  422. case CRYPTO_PK_RSA:
  423. return RSA_public_encrypt(fromlen, from, to, (RSA *)env->key, padding);
  424. default:
  425. return -1;
  426. }
  427. }
  428. int crypto_pk_private_decrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding)
  429. {
  430. assert(env && from && to);
  431. switch(env->type) {
  432. case CRYPTO_PK_RSA:
  433. if (!(((RSA*)env->key)->p))
  434. return -1;
  435. return RSA_private_decrypt(fromlen, from, to, (RSA *)env->key, padding);
  436. default:
  437. return -1;
  438. }
  439. }
  440. int crypto_pk_public_checksig(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to)
  441. {
  442. assert(env && from && to);
  443. switch(env->type) {
  444. case CRYPTO_PK_RSA:
  445. return RSA_public_decrypt(fromlen, from, to, (RSA *)env->key,
  446. RSA_PKCS1_PADDING);
  447. default:
  448. return -1;
  449. }
  450. }
  451. int crypto_pk_private_sign(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to)
  452. {
  453. assert(env && from && to);
  454. switch(env->type) {
  455. case CRYPTO_PK_RSA:
  456. if (!(((RSA*)env->key)->p))
  457. return -1;
  458. return RSA_private_encrypt(fromlen, from, to, (RSA *)env->key,
  459. RSA_PKCS1_PADDING);
  460. default:
  461. return -1;
  462. }
  463. }
  464. /* symmetric crypto */
  465. int crypto_cipher_generate_key(crypto_cipher_env_t *env)
  466. {
  467. int key_len;
  468. assert(env);
  469. key_len = crypto_cipher_key_length(env->type);
  470. if (key_len > 0)
  471. return crypto_rand(key_len, env->key);
  472. else if (key_len == 0)
  473. return 0;
  474. else
  475. return -1;
  476. }
  477. int crypto_cipher_set_iv(crypto_cipher_env_t *env, unsigned char *iv)
  478. {
  479. int iv_len;
  480. assert(env && iv);
  481. iv_len = crypto_cipher_iv_length(env->type);
  482. if (!iv_len)
  483. return 0;
  484. if (!env->iv)
  485. return -1;
  486. memcpy((void*)env->iv, (void*)iv, iv_len);
  487. return 0;
  488. }
  489. int crypto_cipher_set_key(crypto_cipher_env_t *env, unsigned char *key)
  490. {
  491. int key_len;
  492. assert(env && key);
  493. key_len = crypto_cipher_key_length(env->type);
  494. if (!key_len)
  495. return 0;
  496. if (!env->key)
  497. return -1;
  498. memcpy((void*)env->key, (void*)key, key_len);
  499. return 0;
  500. }
  501. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env)
  502. {
  503. assert(env);
  504. if (crypto_cipher_evp_cipher(env->type, 1)) {
  505. RETURN_SSL_OUTCOME(EVP_EncryptInit((EVP_CIPHER_CTX *)env->aux,
  506. crypto_cipher_evp_cipher(env->type, 1),
  507. env->key, env->iv));
  508. } else {
  509. return -1;
  510. }
  511. }
  512. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env)
  513. {
  514. assert(env);
  515. if (crypto_cipher_evp_cipher(env->type, 0)) {
  516. RETURN_SSL_OUTCOME(EVP_EncryptInit((EVP_CIPHER_CTX *)env->aux,
  517. crypto_cipher_evp_cipher(env->type, 0),
  518. env->key, env->iv));
  519. } else {
  520. return -1;
  521. }
  522. }
  523. int crypto_cipher_encrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to)
  524. {
  525. int tolen;
  526. assert(env && from && to);
  527. RETURN_SSL_OUTCOME(EVP_EncryptUpdate((EVP_CIPHER_CTX *)env->aux, to, &tolen, from, fromlen));
  528. }
  529. int crypto_cipher_decrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to)
  530. {
  531. int tolen;
  532. assert(env && from && to);
  533. RETURN_SSL_OUTCOME(EVP_DecryptUpdate((EVP_CIPHER_CTX *)env->aux, to, &tolen, from, fromlen));
  534. }
  535. /* SHA-1 */
  536. int crypto_SHA_digest(unsigned char *m, int len, unsigned char *digest)
  537. {
  538. assert(m && digest);
  539. return (SHA1(m,len,digest) == NULL);
  540. }
  541. struct crypto_dh_env_st {
  542. DH *dh;
  543. };
  544. static BIGNUM *dh_param_p = NULL;
  545. static BIGNUM *dh_param_g = NULL;
  546. static void init_dh_param() {
  547. BIGNUM *p, *g;
  548. int r;
  549. if (dh_param_p && dh_param_g)
  550. return;
  551. p = BN_new();
  552. g = BN_new();
  553. assert(p && g);
  554. #if 0
  555. /* This is from draft-ietf-ipsec-ike-modp-groups-05.txt. It's a safe
  556. prime, and supposedly it equals:
  557. 2^1536 - 2^1472 - 1 + 2^64 * { [2^1406 pi] + 741804 }
  558. */
  559. r = BN_hex2bn(&p,
  560. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
  561. "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
  562. "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
  563. "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
  564. "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
  565. "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
  566. "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
  567. "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF");
  568. #endif
  569. /* This is from rfc2409, section 6.2. It's a safe prime, and
  570. supposedly it equals:
  571. 2^1024 - 2^960 - 1 + 2^64 * { [2^894 pi] + 129093 }.
  572. */
  573. r = BN_hex2bn(&p,
  574. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
  575. "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
  576. "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
  577. "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
  578. "49286651ECE65381FFFFFFFFFFFFFFFF");
  579. assert(r);
  580. r = BN_set_word(g, 2);
  581. assert(r);
  582. dh_param_p = p;
  583. dh_param_g = g;
  584. }
  585. crypto_dh_env_t *crypto_dh_new()
  586. {
  587. crypto_dh_env_t *res = NULL;
  588. if (!dh_param_p)
  589. init_dh_param();
  590. res = tor_malloc(sizeof(crypto_dh_env_t));
  591. res->dh = NULL;
  592. if (!(res->dh = DH_new()))
  593. goto err;
  594. if (!(res->dh->p = BN_dup(dh_param_p)))
  595. goto err;
  596. if (!(res->dh->g = BN_dup(dh_param_g)))
  597. goto err;
  598. return res;
  599. err:
  600. if (res && res->dh) DH_free(res->dh); /* frees p and g too */
  601. if (res) free(res);
  602. return NULL;
  603. }
  604. int crypto_dh_get_bytes(crypto_dh_env_t *dh)
  605. {
  606. assert(dh);
  607. return DH_size(dh->dh);
  608. }
  609. int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey, int pubkey_len)
  610. {
  611. int bytes;
  612. assert(dh);
  613. if (!DH_generate_key(dh->dh))
  614. return -1;
  615. assert(dh->dh->pub_key);
  616. bytes = BN_num_bytes(dh->dh->pub_key);
  617. if (pubkey_len < bytes)
  618. return -1;
  619. memset(pubkey, 0, pubkey_len);
  620. BN_bn2bin(dh->dh->pub_key, pubkey+(pubkey_len-bytes));
  621. return 0;
  622. }
  623. int crypto_dh_compute_secret(crypto_dh_env_t *dh,
  624. char *pubkey, int pubkey_len,
  625. char *secret_out)
  626. {
  627. BIGNUM *pubkey_bn;
  628. int secret_len;
  629. assert(dh);
  630. if (!(pubkey_bn = BN_bin2bn(pubkey, pubkey_len, NULL)))
  631. return -1;
  632. secret_len = DH_compute_key(secret_out, pubkey_bn, dh->dh);
  633. BN_free(pubkey_bn);
  634. if (secret_len == -1)
  635. return -1;
  636. return secret_len;
  637. }
  638. void crypto_dh_free(crypto_dh_env_t *dh)
  639. {
  640. assert(dh && dh->dh);
  641. DH_free(dh->dh);
  642. free(dh);
  643. }
  644. /* random numbers */
  645. int crypto_seed_rng()
  646. {
  647. static char *filenames[] = {
  648. "/dev/srandom", "/dev/urandom", "/dev/random", NULL
  649. };
  650. int i;
  651. char buf[21];
  652. char *cp;
  653. FILE *f;
  654. for (i = 0; filenames[i]; ++i) {
  655. f = fopen(filenames[i], "rb");
  656. if (!f) continue;
  657. log(LOG_INFO, "Seeding RNG from %s", filenames[i]);
  658. buf[20]='\xff';
  659. cp = fgets(buf, 20, f);
  660. fclose(f);
  661. if (!cp || buf[20]) {
  662. log(LOG_INFO, "Error reading from entropy source");
  663. return -1;
  664. }
  665. RAND_seed(buf, 20);
  666. return 0;
  667. }
  668. log(LOG_INFO, "Cannot seed RNG -- no entropy source found.");
  669. return -1;
  670. }
  671. int crypto_rand(unsigned int n, unsigned char *to)
  672. {
  673. assert(to);
  674. return (RAND_bytes(to, n) != 1);
  675. }
  676. int crypto_pseudo_rand(unsigned int n, unsigned char *to)
  677. {
  678. assert(to);
  679. return (RAND_pseudo_bytes(to, n) == -1);
  680. }
  681. /* errors */
  682. char *crypto_perror()
  683. {
  684. return (char *)ERR_reason_error_string(ERR_get_error());
  685. }
  686. int
  687. base64_encode(char *dest, int destlen, char *src, int srclen)
  688. {
  689. EVP_ENCODE_CTX ctx;
  690. int len, ret;
  691. /* 48 bytes of input -> 64 bytes of output plus newline.
  692. Plus one more byte, in case I'm wrong.
  693. */
  694. if (destlen < ((srclen/48)+1)*66)
  695. return -1;
  696. EVP_EncodeInit(&ctx);
  697. EVP_EncodeUpdate(&ctx, dest, &len, src, srclen);
  698. EVP_EncodeFinal(&ctx, dest+len, &ret);
  699. ret += len;
  700. return ret;
  701. }
  702. int
  703. base64_decode(char *dest, int destlen, char *src, int srclen)
  704. {
  705. EVP_ENCODE_CTX ctx;
  706. int len, ret;
  707. /* 64 bytes of input -> *up to* 48 bytes of output.
  708. Plus one more byte, in caes I'm wrong.
  709. */
  710. if (destlen < ((srclen/64)+1)*49)
  711. return -1;
  712. EVP_DecodeInit(&ctx);
  713. EVP_DecodeUpdate(&ctx, dest, &len, src, srclen);
  714. EVP_DecodeFinal(&ctx, dest, &ret);
  715. ret += len;
  716. return ret;
  717. }