crypto.c 20 KB

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