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