crypto.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 int
  36. crypto_cipher_iv_length(int type) {
  37. switch(type)
  38. {
  39. case CRYPTO_CIPHER_IDENTITY: return 0;
  40. case CRYPTO_CIPHER_DES: return 8;
  41. case CRYPTO_CIPHER_RC4: return 16;
  42. case CRYPTO_CIPHER_3DES: return 8;
  43. default: assert(0); return -1;
  44. }
  45. }
  46. static inline int
  47. crypto_cipher_key_length(int type) {
  48. switch(type)
  49. {
  50. case CRYPTO_CIPHER_IDENTITY: return 0;
  51. case CRYPTO_CIPHER_DES: return 8;
  52. case CRYPTO_CIPHER_RC4: return 16;
  53. case CRYPTO_CIPHER_3DES: return 16;
  54. default: assert(0); return -1;
  55. }
  56. }
  57. static inline EVP_CIPHER *
  58. crypto_cipher_evp_cipher(int type, int enc) {
  59. switch(type)
  60. {
  61. case CRYPTO_CIPHER_IDENTITY: return EVP_enc_null();
  62. case CRYPTO_CIPHER_DES: return EVP_des_ofb();
  63. case CRYPTO_CIPHER_RC4: return EVP_rc4();
  64. case CRYPTO_CIPHER_3DES: return EVP_des_ede_ofb();
  65. default: return NULL;
  66. }
  67. }
  68. int crypto_global_init()
  69. {
  70. ERR_load_crypto_strings();
  71. return 0;
  72. }
  73. int crypto_global_cleanup()
  74. {
  75. ERR_free_strings();
  76. return 0;
  77. }
  78. crypto_pk_env_t *crypto_new_pk_env(int type)
  79. {
  80. crypto_pk_env_t *env;
  81. env = (crypto_pk_env_t *)malloc(sizeof(crypto_pk_env_t));
  82. if (!env)
  83. return 0;
  84. env->type = type;
  85. env->refs = 1;
  86. env->key = NULL;
  87. env->aux = NULL;
  88. switch(type) {
  89. case CRYPTO_PK_RSA:
  90. env->key = (unsigned char *)RSA_new();
  91. if (!env->key) {
  92. free((void *)env);
  93. return NULL;
  94. }
  95. break;
  96. default:
  97. free((void *)env);
  98. return NULL;
  99. break;
  100. }
  101. return env;
  102. }
  103. void crypto_free_pk_env(crypto_pk_env_t *env)
  104. {
  105. assert(env);
  106. if(--env->refs > 0)
  107. return;
  108. switch(env->type) {
  109. case CRYPTO_PK_RSA:
  110. if (env->key)
  111. RSA_free((RSA *)env->key);
  112. break;
  113. default:
  114. break;
  115. }
  116. free((void *)env);
  117. return;
  118. }
  119. /* Create a new crypto_cipher_env_t for a given onion cipher type, key,
  120. * iv, and encryption flag (1=encrypt, 0=decrypt). Return the crypto object
  121. * on success; NULL on failure.
  122. */
  123. crypto_cipher_env_t *
  124. crypto_create_init_cipher(int cipher_type, char *key, char *iv, int encrypt_mode)
  125. {
  126. int r;
  127. crypto_cipher_env_t *crypto = NULL;
  128. if (! (crypto = crypto_new_cipher_env(cipher_type))) {
  129. log(LOG_ERR, "Unable to allocate crypto object");
  130. return NULL;
  131. }
  132. if (crypto_cipher_set_key(crypto, key)) {
  133. log(LOG_ERR, "Unable to set key: %s", crypto_perror());
  134. goto error;
  135. }
  136. if (crypto_cipher_set_iv(crypto, iv)) {
  137. log(LOG_ERR, "Unable to set iv: %s", crypto_perror());
  138. goto error;
  139. }
  140. if (encrypt_mode)
  141. r = crypto_cipher_encrypt_init_cipher(crypto);
  142. else
  143. r = crypto_cipher_decrypt_init_cipher(crypto);
  144. if (r) {
  145. log(LOG_ERR, "Unabble to initialize cipher: %s", crypto_perror());
  146. goto error;
  147. }
  148. return crypto;
  149. error:
  150. if (crypto)
  151. crypto_free_cipher_env(crypto);
  152. return NULL;
  153. }
  154. crypto_cipher_env_t *crypto_new_cipher_env(int type)
  155. {
  156. crypto_cipher_env_t *env;
  157. int iv_len, key_len;
  158. env = (crypto_cipher_env_t *)malloc(sizeof(crypto_cipher_env_t));
  159. if (!env)
  160. return NULL;
  161. env->type = type;
  162. env->key = NULL;
  163. env->iv = NULL;
  164. env->aux = NULL;
  165. iv_len = crypto_cipher_iv_length(type);
  166. key_len = crypto_cipher_key_length(type);
  167. if (! crypto_cipher_evp_cipher(type,0))
  168. /* This is not an openssl cipher */
  169. goto err;
  170. else {
  171. env->aux = (unsigned char *)malloc(sizeof(EVP_CIPHER_CTX));
  172. EVP_CIPHER_CTX_init((EVP_CIPHER_CTX *)env->aux);
  173. }
  174. if (iv_len && !(env->iv = (unsigned char *)malloc(iv_len)))
  175. goto err;
  176. if (key_len && !(env->key = (unsigned char *)malloc(key_len)))
  177. goto err;
  178. return env;
  179. err:
  180. if (env->key)
  181. free(env->key);
  182. if (env->iv)
  183. free(env->iv);
  184. if (env->aux)
  185. free(env->aux);
  186. if (env)
  187. free(env);
  188. return NULL;
  189. }
  190. void crypto_free_cipher_env(crypto_cipher_env_t *env)
  191. {
  192. assert(env);
  193. if (crypto_cipher_evp_cipher(env->type,0)) {
  194. /* This is an openssl cipher */
  195. assert(env->aux);
  196. EVP_CIPHER_CTX_cleanup((EVP_CIPHER_CTX *)env->aux);
  197. }
  198. if (env->aux)
  199. free((void *)env->aux);
  200. if (env->iv)
  201. free((void *)env->iv);
  202. if (env->key)
  203. free((void *)env->key);
  204. free((void *)env);
  205. }
  206. /* public key crypto */
  207. int crypto_pk_generate_key(crypto_pk_env_t *env)
  208. {
  209. assert(env);
  210. switch(env->type) {
  211. case CRYPTO_PK_RSA:
  212. if (env->key)
  213. RSA_free((RSA *)env->key);
  214. env->key = (unsigned char *)RSA_generate_key(1024,65537, NULL, NULL);
  215. if (!env->key)
  216. return -1;
  217. break;
  218. default:
  219. return -1;
  220. }
  221. return 0;
  222. }
  223. int crypto_pk_read_private_key_from_file(crypto_pk_env_t *env, FILE *src)
  224. {
  225. assert(env && src);
  226. switch(env->type) {
  227. case CRYPTO_PK_RSA:
  228. if (env->key)
  229. RSA_free((RSA *)env->key);
  230. env->key = (unsigned char *)PEM_read_RSAPrivateKey(src, NULL, 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_filename(crypto_pk_env_t *env, unsigned char *keyfile)
  240. {
  241. FILE *f_pr;
  242. int retval = 0;
  243. assert(env && keyfile);
  244. if (strspn(keyfile,CONFIG_LEGAL_FILENAME_CHARACTERS) == strlen(keyfile)) /* filename contains legal characters only */
  245. {
  246. /* open the keyfile */
  247. f_pr=fopen(keyfile,"rb");
  248. if (!f_pr)
  249. return -1;
  250. /* read the private key */
  251. retval = crypto_pk_read_private_key_from_file(env, f_pr);
  252. fclose(f_pr);
  253. if (retval == -1)
  254. {
  255. log(LOG_ERR,"Error reading private key : %s",crypto_perror());
  256. return -1;
  257. }
  258. /* check the private key */
  259. retval = crypto_pk_check_key(env);
  260. if (retval == 0)
  261. {
  262. log(LOG_ERR,"Private key read but is invalid : %s.", crypto_perror());
  263. return -1;
  264. }
  265. else if (retval == -1)
  266. {
  267. log(LOG_ERR,"Private key read but validity checking failed : %s",crypto_perror());
  268. return -1;
  269. }
  270. else if (retval == 1)
  271. {
  272. return 0;
  273. }
  274. } /* filename contains legal characters only */
  275. return -1; /* report error */
  276. }
  277. int crypto_pk_read_public_key_from_file(crypto_pk_env_t *env, FILE *src)
  278. {
  279. assert(env && src);
  280. switch(env->type) {
  281. case CRYPTO_PK_RSA:
  282. if(env->key)
  283. RSA_free((RSA *)env->key);
  284. env->key = (unsigned char *)PEM_read_RSAPublicKey(src, NULL, NULL, NULL);
  285. if (!env->key)
  286. return -1;
  287. break;
  288. default :
  289. return -1;
  290. }
  291. return 0;
  292. }
  293. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, int *len) {
  294. BUF_MEM *buf;
  295. BIO *b;
  296. assert(env && env->key && dest);
  297. switch(env->type) {
  298. case CRYPTO_PK_RSA:
  299. b = BIO_new(BIO_s_mem()); /* Create a memory BIO */
  300. /* Now you can treat b as if it were a file. Just use the
  301. * * PEM_*_bio_* functions instead of the non-bio variants.
  302. * */
  303. if(!PEM_write_bio_RSAPublicKey(b, (RSA *)env->key))
  304. return -1;
  305. BIO_get_mem_ptr(b, &buf);
  306. BIO_set_close(b, BIO_NOCLOSE); /* so BIO_free doesn't free buf */
  307. BIO_free(b);
  308. *dest = malloc(buf->length+1);
  309. if(!*dest)
  310. return -1;
  311. memcpy(*dest, buf->data, buf->length);
  312. (*dest)[buf->length] = 0; /* null terminate it */
  313. *len = buf->length;
  314. BUF_MEM_free(buf);
  315. break;
  316. default:
  317. return -1;
  318. }
  319. return 0;
  320. }
  321. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, char *src, int len) {
  322. BIO *b;
  323. assert(env && src);
  324. switch(env->type) {
  325. case CRYPTO_PK_RSA:
  326. b = BIO_new(BIO_s_mem()); /* Create a memory BIO */
  327. BIO_write(b, src, len);
  328. RSA_free((RSA *)env->key);
  329. env->key = (unsigned char *)PEM_read_bio_RSAPublicKey(b, NULL, NULL, NULL);
  330. if(!env->key)
  331. return -1;
  332. BIO_free(b);
  333. break;
  334. default:
  335. return -1;
  336. }
  337. return 0;
  338. }
  339. int crypto_pk_write_private_key_to_file(crypto_pk_env_t *env, FILE *dest)
  340. {
  341. assert(env && dest);
  342. switch(env->type) {
  343. case CRYPTO_PK_RSA:
  344. if (!env->key)
  345. return -1;
  346. if (PEM_write_RSAPrivateKey(dest, (RSA *)env->key, NULL, NULL, 0,0, NULL) == 0)
  347. return -1;
  348. break;
  349. default :
  350. return -1;
  351. }
  352. return 0;
  353. }
  354. int crypto_pk_write_public_key_to_file(crypto_pk_env_t *env, FILE *dest)
  355. {
  356. assert(env && dest);
  357. switch(env->type) {
  358. case CRYPTO_PK_RSA:
  359. if (!env->key)
  360. return -1;
  361. if (PEM_write_RSAPublicKey(dest, (RSA *)env->key) == 0)
  362. return -1;
  363. break;
  364. default :
  365. return -1;
  366. }
  367. return 0;
  368. }
  369. int crypto_pk_check_key(crypto_pk_env_t *env)
  370. {
  371. assert(env);
  372. switch(env->type) {
  373. case CRYPTO_PK_RSA:
  374. return RSA_check_key((RSA *)env->key);
  375. default:
  376. return -1;
  377. }
  378. }
  379. int crypto_pk_set_key(crypto_pk_env_t *env, unsigned char *key)
  380. {
  381. assert(env && key);
  382. switch(env->type) {
  383. case CRYPTO_PK_RSA:
  384. if (!env->key)
  385. return -1;
  386. /* XXX BUG XXX you can't memcpy an RSA, it's got a bunch of subpointers */
  387. assert(0);
  388. memcpy((void *)env->key, (void *)key, sizeof(RSA));
  389. break;
  390. default :
  391. return -1;
  392. }
  393. return 0;
  394. }
  395. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b) {
  396. int result;
  397. if (!a || !b)
  398. return -1;
  399. if (!a->key || !b->key)
  400. return -1;
  401. if (a->type != b->type)
  402. return -1;
  403. switch(a->type) {
  404. case CRYPTO_PK_RSA:
  405. assert(((RSA *)a->key)->n && ((RSA *)a->key)->e && ((RSA *)b->key)->n && ((RSA *)b->key)->e);
  406. result = BN_cmp(((RSA *)a->key)->n, ((RSA *)b->key)->n);
  407. if (result)
  408. return result;
  409. return BN_cmp(((RSA *)a->key)->e, ((RSA *)b->key)->e);
  410. default:
  411. return -1;
  412. }
  413. }
  414. int crypto_pk_keysize(crypto_pk_env_t *env)
  415. {
  416. assert(env && env->key);
  417. return RSA_size((RSA *)env->key);
  418. }
  419. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *env) {
  420. assert(env && env->key);
  421. switch(env->type) {
  422. case CRYPTO_PK_RSA:
  423. env->refs++;
  424. break;
  425. default:
  426. return NULL;
  427. }
  428. return env;
  429. }
  430. int crypto_pk_public_encrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding)
  431. {
  432. assert(env && from && to);
  433. switch(env->type) {
  434. case CRYPTO_PK_RSA:
  435. return RSA_public_encrypt(fromlen, from, to, (RSA *)env->key, padding);
  436. default:
  437. return -1;
  438. }
  439. }
  440. int crypto_pk_private_decrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding)
  441. {
  442. assert(env && from && to);
  443. switch(env->type) {
  444. case CRYPTO_PK_RSA:
  445. if (!(((RSA*)env->key)->p))
  446. return -1;
  447. return RSA_private_decrypt(fromlen, from, to, (RSA *)env->key, padding);
  448. default:
  449. return -1;
  450. }
  451. }
  452. /* symmetric crypto */
  453. int crypto_cipher_generate_key(crypto_cipher_env_t *env)
  454. {
  455. int key_len;
  456. assert(env);
  457. key_len = crypto_cipher_key_length(env->type);
  458. if (key_len > 0)
  459. return crypto_rand(key_len, env->key);
  460. else if (key_len == 0)
  461. return 0;
  462. else
  463. return -1;
  464. }
  465. int crypto_cipher_set_iv(crypto_cipher_env_t *env, unsigned char *iv)
  466. {
  467. int iv_len;
  468. assert(env && iv);
  469. iv_len = crypto_cipher_iv_length(env->type);
  470. if (!iv_len)
  471. return 0;
  472. if (!env->iv)
  473. return -1;
  474. memcpy((void*)env->iv, (void*)iv, iv_len);
  475. return 0;
  476. }
  477. int crypto_cipher_set_key(crypto_cipher_env_t *env, unsigned char *key)
  478. {
  479. int key_len;
  480. assert(env && key);
  481. key_len = crypto_cipher_key_length(env->type);
  482. if (!key_len)
  483. return 0;
  484. if (!env->key)
  485. return -1;
  486. memcpy((void*)env->key, (void*)key, key_len);
  487. return 0;
  488. }
  489. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env)
  490. {
  491. assert(env);
  492. if (crypto_cipher_evp_cipher(env->type, 1)) {
  493. RETURN_SSL_OUTCOME(EVP_EncryptInit((EVP_CIPHER_CTX *)env->aux,
  494. crypto_cipher_evp_cipher(env->type, 1),
  495. env->key, env->iv));
  496. } else {
  497. return -1;
  498. }
  499. }
  500. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env)
  501. {
  502. assert(env);
  503. if (crypto_cipher_evp_cipher(env->type, 0)) {
  504. RETURN_SSL_OUTCOME(EVP_EncryptInit((EVP_CIPHER_CTX *)env->aux,
  505. crypto_cipher_evp_cipher(env->type, 0),
  506. env->key, env->iv));
  507. } else {
  508. return -1;
  509. }
  510. }
  511. int crypto_cipher_encrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to)
  512. {
  513. int tolen;
  514. assert(env && from && to);
  515. RETURN_SSL_OUTCOME(EVP_EncryptUpdate((EVP_CIPHER_CTX *)env->aux, to, &tolen, from, fromlen));
  516. }
  517. int crypto_cipher_decrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to)
  518. {
  519. int tolen;
  520. assert(env && from && to);
  521. RETURN_SSL_OUTCOME(EVP_DecryptUpdate((EVP_CIPHER_CTX *)env->aux, to, &tolen, from, fromlen));
  522. }
  523. /* SHA-1 */
  524. int crypto_SHA_digest(unsigned char *m, int len, unsigned char *digest)
  525. {
  526. assert(m && digest);
  527. return (SHA1(m,len,digest) == NULL);
  528. }
  529. struct crypto_dh_env_st {
  530. DH *dh;
  531. };
  532. static BIGNUM *dh_param_p = NULL;
  533. static BIGNUM *dh_param_g = NULL;
  534. static void init_dh_param() {
  535. BIGNUM *p, *g;
  536. int r;
  537. if (dh_param_p && dh_param_g)
  538. return;
  539. p = BN_new();
  540. g = BN_new();
  541. assert(p && g);
  542. /* This is from draft-ietf-ipsec-ike-modp-groups-05.txt. It's a safe
  543. prime, and supposedly it equals:
  544. 2^1536 - 2^1472 - 1 + 2^64 * { [2^1406 pi] + 741804 }
  545. */
  546. r = BN_hex2bn(&p,
  547. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
  548. "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
  549. "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
  550. "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
  551. "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
  552. "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
  553. "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
  554. "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF");
  555. assert(r);
  556. r = BN_set_word(g, 2);
  557. assert(r);
  558. dh_param_p = p;
  559. dh_param_g = g;
  560. }
  561. crypto_dh_env_t *crypto_dh_new()
  562. {
  563. crypto_dh_env_t *res = NULL;
  564. if (!dh_param_p)
  565. init_dh_param();
  566. if (!(res = malloc(sizeof(crypto_dh_env_t))))
  567. goto err;
  568. res->dh = NULL;
  569. if (!(res->dh = DH_new()))
  570. goto err;
  571. if (!(res->dh->p = BN_dup(dh_param_p)))
  572. goto err;
  573. if (!(res->dh->g = BN_dup(dh_param_g)))
  574. goto err;
  575. return res;
  576. err:
  577. if (res && res->dh) DH_free(res->dh); /* frees p and g too */
  578. if (res) free(res);
  579. return NULL;
  580. }
  581. int crypto_dh_get_bytes(crypto_dh_env_t *dh)
  582. {
  583. assert(dh);
  584. return DH_size(dh->dh);
  585. }
  586. int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey, int pubkey_len)
  587. {
  588. int bytes;
  589. assert(dh);
  590. if (!DH_generate_key(dh->dh))
  591. return -1;
  592. assert(dh->dh->pub_key);
  593. bytes = BN_num_bytes(dh->dh->pub_key);
  594. if (pubkey_len < bytes)
  595. return -1;
  596. memset(pubkey, 0, pubkey_len);
  597. BN_bn2bin(dh->dh->pub_key, pubkey+(pubkey_len-bytes));
  598. return 0;
  599. }
  600. int crypto_dh_compute_secret(crypto_dh_env_t *dh,
  601. char *pubkey, int pubkey_len,
  602. char *secret_out)
  603. {
  604. BIGNUM *pubkey_bn;
  605. int secret_len;
  606. assert(dh);
  607. if (!(pubkey_bn = BN_bin2bn(pubkey, pubkey_len, NULL)))
  608. return -1;
  609. secret_len = DH_compute_key(secret_out, pubkey_bn, dh->dh);
  610. BN_free(pubkey_bn);
  611. if (secret_len == -1)
  612. return -1;
  613. return 0;
  614. }
  615. void crypto_dh_free(crypto_dh_env_t *dh)
  616. {
  617. assert(dh && dh->dh);
  618. DH_free(dh->dh);
  619. free(dh);
  620. }
  621. /* random numbers */
  622. int crypto_rand(unsigned int n, unsigned char *to)
  623. {
  624. assert(to);
  625. return (RAND_bytes(to, n) != 1);
  626. }
  627. int crypto_pseudo_rand(unsigned int n, unsigned char *to)
  628. {
  629. assert(to);
  630. return (RAND_pseudo_bytes(to, n) == -1);
  631. }
  632. /* errors */
  633. char *crypto_perror()
  634. {
  635. return (char *)ERR_reason_error_string(ERR_get_error());
  636. }