cryptothread.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Name: cryptothread.c
  2. *
  3. * This function contains the code necessary for using OpenSSL in a thread-safe
  4. * manner.
  5. *
  6. * Slitheen - a decoy routing system for censorship resistance
  7. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, version 3.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Additional permission under GNU GPL version 3 section 7
  22. *
  23. * If you modify this Program, or any covered work, by linking or combining
  24. * it with the OpenSSL library (or a modified version of that library),
  25. * containing parts covered by the terms of [name of library's license],
  26. * the licensors of this Program grant you additional permission to convey
  27. * the resulting work. {Corresponding Source for a non-source form of such
  28. * a combination shall include the source code for the parts of the OpenSSL
  29. * library used as well as that of the covered work.}
  30. */
  31. #include <pthread.h>
  32. #include <openssl/crypto.h>
  33. #include "cryptothread.h"
  34. static pthread_mutex_t *crypto_locks;
  35. static long *lock_count;
  36. void init_crypto_locks(void){
  37. crypto_locks = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
  38. if(!crypto_locks)
  39. exit(1);
  40. lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
  41. if(!lock_count)
  42. exit(1);
  43. for (int i = 0; i < CRYPTO_num_locks(); i++) {
  44. lock_count[i] = 0;
  45. pthread_mutex_init(&(crypto_locks[i]), NULL);
  46. }
  47. CRYPTO_THREADID_set_callback(pthreads_thread_id);
  48. CRYPTO_set_locking_callback(pthreads_locking_callback);
  49. }
  50. void crypto_locks_cleanup(void){
  51. int i;
  52. CRYPTO_set_locking_callback(NULL);
  53. for (i = 0; i < CRYPTO_num_locks(); i++) {
  54. pthread_mutex_destroy(&(crypto_locks[i]));
  55. }
  56. OPENSSL_free(crypto_locks);
  57. OPENSSL_free(lock_count);
  58. }
  59. /** If the mode is CRYPTO_LOCK, the lock indicated by type will be acquired, otherwise it will be released */
  60. void pthreads_locking_callback(int mode, int type, const char *file, int line){
  61. if(mode & CRYPTO_LOCK){
  62. pthread_mutex_lock(&(crypto_locks[type]));
  63. lock_count[type]++;
  64. } else {
  65. pthread_mutex_unlock(&(crypto_locks[type]));
  66. }
  67. }
  68. void pthreads_thread_id(CRYPTO_THREADID *tid){
  69. CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
  70. }