cryptothread.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 the OpenSSL Licence and the
  26. * SSLeay license, the licensors of this Program grant you additional
  27. * permission to convey the resulting work. Corresponding Source for a
  28. * non-source form of such a combination shall include the source code
  29. * for the parts of the OpenSSL library used as well as that of the covered
  30. * work.
  31. */
  32. #include <pthread.h>
  33. #include <openssl/crypto.h>
  34. #include "cryptothread.h"
  35. static pthread_mutex_t *crypto_locks;
  36. static long *lock_count;
  37. void init_crypto_locks(void){
  38. crypto_locks = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
  39. if(!crypto_locks)
  40. exit(1);
  41. lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
  42. if(!lock_count)
  43. exit(1);
  44. for (int i = 0; i < CRYPTO_num_locks(); i++) {
  45. lock_count[i] = 0;
  46. pthread_mutex_init(&(crypto_locks[i]), NULL);
  47. }
  48. CRYPTO_THREADID_set_callback(pthreads_thread_id);
  49. CRYPTO_set_locking_callback(pthreads_locking_callback);
  50. }
  51. void crypto_locks_cleanup(void){
  52. int i;
  53. CRYPTO_set_locking_callback(NULL);
  54. for (i = 0; i < CRYPTO_num_locks(); i++) {
  55. pthread_mutex_destroy(&(crypto_locks[i]));
  56. }
  57. OPENSSL_free(crypto_locks);
  58. OPENSSL_free(lock_count);
  59. }
  60. /** If the mode is CRYPTO_LOCK, the lock indicated by type will be acquired, otherwise it will be released */
  61. void pthreads_locking_callback(int mode, int type, const char *file, int line){
  62. if(mode & CRYPTO_LOCK){
  63. pthread_mutex_lock(&(crypto_locks[type]));
  64. lock_count[type]++;
  65. } else {
  66. pthread_mutex_unlock(&(crypto_locks[type]));
  67. }
  68. }
  69. void pthreads_thread_id(CRYPTO_THREADID *tid){
  70. CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
  71. }