Changeset 14354


Ignore:
Timestamp:
Dec 4, 2014, 11:27:38 AM (8 years ago)
Author:
mikedld
Message:

#4400, #5462: Move random helpers to crypto-utils

On a way to factoring out OpenSSL support to a standalone file to ease
addition of other crypto libraries support in the future, move helpers
providing random numbers/data generation to crypto-utils.{c,h}. OpenSSL-
related functionality (generation of cryptographically strong random
data) is moved to crypto-utils-openssl.c.

Rename functions to follow currently accepted style:

  • tr_cryptoRandBuf -> tr_rand_buffer
  • tr_cryptoRandInt -> tr_rand_int
  • tr_cryptoWeakRandInt -> tr_rand_int_weak

Fix rare case of invalid value being returned from tr_rand_int. Return
value for abs(INT_MIN) may be undefined and thus negative, and so
tr_rand_int will return negative value which is incorrect (out of
requested and expected range).

Location:
trunk/libtransmission
Files:
3 added
20 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/Makefile.am

    r14332 r14354  
    2929  ConvertUTF.c \
    3030  crypto.c \
     31  crypto-utils.c \
     32  crypto-utils-openssl.c \
    3133  error.c \
    3234  fdlimit.c \
     
    9092  ConvertUTF.h \
    9193  crypto.h \
     94  crypto-utils.h \
    9295  completion.h \
    9396  error.h \
  • trunk/libtransmission/announcer-udp.c

    r14241 r14354  
    2020#include "announcer.h"
    2121#include "announcer-common.h"
    22 #include "crypto.h" /* tr_cryptoRandBuf () */
     22#include "crypto-utils.h" /* tr_rand_buffer () */
    2323#include "log.h"
    2424#include "peer-io.h"
     
    109109{
    110110    tau_transaction_t tmp;
    111     tr_cryptoRandBuf (&tmp, sizeof (tau_transaction_t));
     111    tr_rand_buffer (&tmp, sizeof (tau_transaction_t));
    112112    return tmp;
    113113}
  • trunk/libtransmission/announcer.c

    r14241 r14354  
    2222#include "announcer.h"
    2323#include "announcer-common.h"
    24 #include "crypto.h" /* tr_cryptoRandInt (), tr_cryptoWeakRandInt () */
     24#include "crypto-utils.h" /* tr_rand_int (), tr_rand_int_weak () */
    2525#include "log.h"
    2626#include "peer-mgr.h" /* tr_peerMgrCompactToPex () */
     
    168168    a = tr_new0 (tr_announcer, 1);
    169169    a->stops = TR_PTR_ARRAY_INIT;
    170     a->key = tr_cryptoRandInt (INT_MAX);
     170    a->key = tr_rand_int (INT_MAX);
    171171    a->session = session;
    172172    a->slotsAvailable = MAX_CONCURRENT_TASKS;
     
    350350    tier->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
    351351    tier->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
    352     tier->scrapeAt = get_next_scrape_time (tor->session, tier, tr_cryptoWeakRandInt (180));
     352    tier->scrapeAt = get_next_scrape_time (tor->session, tier, tr_rand_int_weak (180));
    353353    tier->tor = tor;
    354354}
     
    964964      case 0:  return 0;
    965965      case 1:  return 20;
    966       case 2:  return tr_cryptoWeakRandInt (60) + (60 * 5);
    967       case 3:  return tr_cryptoWeakRandInt (60) + (60 * 15);
    968       case 4:  return tr_cryptoWeakRandInt (60) + (60 * 30);
    969       case 5:  return tr_cryptoWeakRandInt (60) + (60 * 60);
    970       default: return tr_cryptoWeakRandInt (60) + (60 * 120);
     966      case 2:  return tr_rand_int_weak (60) + (60 * 5);
     967      case 3:  return tr_rand_int_weak (60) + (60 * 15);
     968      case 4:  return tr_rand_int_weak (60) + (60 * 30);
     969      case 5:  return tr_rand_int_weak (60) + (60 * 60);
     970      default: return tr_rand_int_weak (60) + (60 * 120);
    971971    }
    972972}
  • trunk/libtransmission/bandwidth.c

    r14241 r14354  
    1313#include "transmission.h"
    1414#include "bandwidth.h"
    15 #include "crypto.h" /* tr_cryptoWeakRandInt () */
     15#include "crypto-utils.h" /* tr_rand_int_weak () */
    1616#include "log.h"
    1717#include "peer-io.h"
     
    211211  while (n > 0)
    212212    {
    213       const int i = tr_cryptoWeakRandInt (n); /* pick a peer at random */
     213      const int i = tr_rand_int_weak (n); /* pick a peer at random */
    214214
    215215      /* value of 3000 bytes chosen so that when using uTP we'll send a full-size
  • trunk/libtransmission/bitfield-test.c

    r14241 r14354  
    1010#include <string.h> /* strlen () */
    1111#include "transmission.h"
    12 #include "crypto.h"
     12#include "crypto-utils.h"
    1313#include "bitfield.h"
    1414#include "utils.h" /* tr_free */
     
    2525  int count1;
    2626  int count2;
    27   const int bitCount = 100 + tr_cryptoWeakRandInt (1000);
     27  const int bitCount = 100 + tr_rand_int_weak (1000);
    2828  tr_bitfield bf;
    2929
    3030  /* generate a random bitfield */
    3131  tr_bitfieldConstruct (&bf, bitCount);
    32   for (i=0, n=tr_cryptoWeakRandInt (bitCount); i<n; ++i)
    33     tr_bitfieldAdd (&bf, tr_cryptoWeakRandInt (bitCount));
    34   begin = tr_cryptoWeakRandInt (bitCount);
     32  for (i=0, n=tr_rand_int_weak (bitCount); i<n; ++i)
     33    tr_bitfieldAdd (&bf, tr_rand_int_weak (bitCount));
     34  begin = tr_rand_int_weak (bitCount);
    3535  do
    36     end = tr_cryptoWeakRandInt (bitCount);
     36    end = tr_rand_int_weak (bitCount);
    3737  while (end == begin);
    3838
  • trunk/libtransmission/crypto-test.c

    r14282 r14354  
    1212#include "transmission.h"
    1313#include "crypto.h"
     14#include "crypto-utils.h"
    1415
    1516#include "libtransmission-test.h"
     
    167168}
    168169
     170static int
     171test_random (void)
     172{
     173  int i;
     174
     175  /* test that tr_rand_int () stays in-bounds */
     176  for (i = 0; i < 100000; ++i)
     177    {
     178      const int val = tr_rand_int (100);
     179      check (val >= 0);
     180      check (val < 100);
     181    }
     182
     183  return 0;
     184}
     185
    169186int
    170187main (void)
     
    173190                             test_encrypt_decrypt,
    174191                             test_sha1,
    175                              test_ssha1 };
     192                             test_ssha1,
     193                             test_random };
    176194
    177195  return runTests (tests, NUM_TESTS (tests));
  • trunk/libtransmission/crypto.c

    r14347 r14354  
    99
    1010#include <assert.h>
    11 #include <inttypes.h> /* uint8_t */
    1211#include <stdarg.h>
    13 #include <stdlib.h> /* abs () */
    1412#include <string.h> /* memcpy (), memset (), strcmp () */
    1513
     
    1917#include <openssl/rc4.h>
    2018#include <openssl/sha.h>
    21 #include <openssl/rand.h>
    2219
    2320#include "transmission.h"
    2421#include "crypto.h"
     22#include "crypto-utils.h"
    2523#include "log.h"
    2624#include "utils.h"
     
    295293}
    296294
    297 int
    298 tr_cryptoRandInt (int upperBound)
    299 {
    300   int noise;
    301   int val;
    302 
    303   assert (upperBound > 0);
    304 
    305   if (RAND_pseudo_bytes ((unsigned char *) &noise, sizeof noise) >= 0)
    306     {
    307       val = abs (noise) % upperBound;
    308     }
    309   else /* fall back to a weaker implementation... */
    310     {
    311       val = tr_cryptoWeakRandInt (upperBound);
    312     }
    313 
    314   return val;
    315 }
    316 
    317 int
    318 tr_cryptoWeakRandInt (int upperBound)
    319 {
    320   static bool init = false;
    321 
    322   assert (upperBound > 0);
    323 
    324   if (!init)
    325     {
    326       srand (tr_time_msec ());
    327       init = true;
    328     }
    329 
    330   return rand () % upperBound;
    331 }
    332 
    333 void
    334 tr_cryptoRandBuf (void * buf, size_t len)
    335 {
    336   if (RAND_pseudo_bytes ((unsigned char*)buf, len) != 1)
    337     logErrorFromSSL ();
    338 }
    339 
    340295/***
    341296****
     
    357312  char buf[2*SHA_DIGEST_LENGTH + saltval_len + 2];
    358313
    359   tr_cryptoRandBuf (salt, saltval_len);
     314  tr_rand_buffer (salt, saltval_len);
    360315  for (i=0; i<saltval_len; ++i)
    361316    salt[i] = salter[ salt[i] % salter_len ];
  • trunk/libtransmission/crypto.h

    r14347 r14354  
    9595
    9696
    97 /** @brief returns a random number in the range of [0...n) */
    98 int tr_cryptoRandInt (int n);
    99 
    100 /**
    101  * @brief returns a pseudorandom number in the range of [0...n)
    102  *
    103  * This is faster, BUT WEAKER, than tr_cryptoRandInt () and never
    104  * be used in sensitive cases.
    105  * @see tr_cryptoRandInt ()
    106  */
    107 int            tr_cryptoWeakRandInt (int n);
    108 
    109 /** @brief fill a buffer with random bytes */
    110 void  tr_cryptoRandBuf (void * buf, size_t len);
    111 
    11297/** @brief generate a SSHA password from its plaintext source */
    11398char*  tr_ssha1 (const void * plaintext);
  • trunk/libtransmission/file-win32.c

    r14330 r14354  
    1515
    1616#include "transmission.h"
    17 #include "crypto.h" /* tr_cryptoRandInt () */
     17#include "crypto-utils.h" /* tr_rand_int () */
    1818#include "file.h"
    1919#include "utils.h"
     
    206206      while (i > 0 && path_template[i - 1] == 'X')
    207207        {
    208           const int c = tr_cryptoRandInt (26 + 26 + 10);
     208          const int c = tr_rand_int (26 + 26 + 10);
    209209          path[i - 1] = c < 26 ? c + 'A' : (c < 26 + 26 ? (c - 26) + 'a' : (c - 26 - 26) + '0');
    210210          --i;
  • trunk/libtransmission/handshake.c

    r14347 r14354  
    1717#include "transmission.h"
    1818#include "clients.h"
    19 #include "crypto.h"
     19#include "crypto-utils.h"
    2020#include "handshake.h"
    2121#include "log.h"
     
    325325
    326326  /* add some bullshit padding */
    327   len = tr_cryptoRandInt (PadA_MAXLEN);
    328   tr_cryptoRandBuf (walk, len);
     327  len = tr_rand_int (PadA_MAXLEN);
     328  tr_rand_buffer (walk, len);
    329329  walk += len;
    330330
     
    749749  memcpy (walk, myKey, len);
    750750  walk += len;
    751   len = tr_cryptoRandInt (PadB_MAXLEN);
    752   tr_cryptoRandBuf (walk, len);
     751  len = tr_rand_int (PadB_MAXLEN);
     752  tr_rand_buffer (walk, len);
    753753  walk += len;
    754754
  • trunk/libtransmission/makemeta-test.c

    r14331 r14354  
    1111
    1212#include "transmission.h"
    13 #include "crypto.h"
     13#include "crypto-utils.h"
    1414#include "file.h"
    1515#include "makemeta.h"
     
    230230
    231231  /* build random payloads */
    232   payloadCount = 1 + tr_cryptoWeakRandInt (maxFileCount);
     232  payloadCount = 1 + tr_rand_int_weak (maxFileCount);
    233233  payloads = tr_new0 (void*, payloadCount);
    234234  payloadSizes = tr_new0 (size_t, payloadCount);
    235235  for (i=0; i<payloadCount; i++)
    236236    {
    237       const size_t n = 1 + tr_cryptoWeakRandInt (maxFileSize);
     237      const size_t n = 1 + tr_rand_int_weak (maxFileSize);
    238238      payloads[i] = tr_new (char, n);
    239       tr_cryptoRandBuf (payloads[i], n);
     239      tr_rand_buffer (payloads[i], n);
    240240      payloadSizes[i] = n;
    241241    }
  • trunk/libtransmission/peer-io.c

    r14347 r14354  
    2121#include "session.h"
    2222#include "bandwidth.h"
    23 #include "crypto.h"
    2423#include "log.h"
    2524#include "net.h"
  • trunk/libtransmission/peer-mgr.c

    r14347 r14354  
    2525#include "clients.h"
    2626#include "completion.h"
    27 #include "crypto.h"
     27#include "crypto-utils.h"
    2828#include "handshake.h"
    2929#include "log.h"
     
    10981098          piece->index = pool[i];
    10991099          piece->requestCount = 0;
    1100           piece->salt = tr_cryptoWeakRandInt (4096);
     1100          piece->salt = tr_rand_int_weak (4096);
    11011101        }
    11021102
     
    18641864  if (a == NULL)
    18651865    {
    1866       const int jitter = tr_cryptoWeakRandInt (60*10);
     1866      const int jitter = tr_rand_int_weak (60*10);
    18671867      a = tr_new0 (struct peer_atom, 1);
    18681868      a->addr = *addr;
     
    29492949              rechoke[rechoke_count].peer = peer;
    29502950              rechoke[rechoke_count].rechoke_state = rechoke_state;
    2951               rechoke[rechoke_count].salt = tr_cryptoWeakRandInt (INT_MAX);
     2951              rechoke[rechoke_count].salt = tr_rand_int_weak (INT_MAX);
    29522952              rechoke_count++;
    29532953            }
     
    30893089          n->wasChoked    = tr_peerMsgsIsPeerChoked (msgs);
    30903090          n->rate         = getRate (s->tor, atom, now);
    3091           n->salt         = tr_cryptoWeakRandInt (INT_MAX);
     3091          n->salt         = tr_rand_int_weak (INT_MAX);
    30923092          n->isChoked     = true;
    30933093        }
     
    31403140      if ((n = tr_ptrArraySize (&randPool)))
    31413141        {
    3142           c = tr_ptrArrayNth (&randPool, tr_cryptoWeakRandInt (n));
     3142          c = tr_ptrArrayNth (&randPool, tr_rand_int_weak (n));
    31433143          c->isChoked = false;
    31443144          s->optimistic = c->msgs;
     
    39833983          if (isPeerCandidate (tor, atom, now))
    39843984            {
    3985               const uint8_t salt = tr_cryptoWeakRandInt (1024);
     3985              const uint8_t salt = tr_rand_int_weak (1024);
    39863986              walk->tor = tor;
    39873987              walk->atom = atom;
  • trunk/libtransmission/peer-msgs.c

    r14347 r14354  
    2121#include "cache.h"
    2222#include "completion.h"
    23 #include "crypto.h" /* tr_sha1 () */
    2423#include "file.h"
    2524#include "log.h"
  • trunk/libtransmission/rpc-server.c

    r14327 r14354  
    2020
    2121#include "transmission.h"
    22 #include "crypto.h" /* tr_cryptoRandBuf (), tr_ssha1_matches () */
     22#include "crypto.h" /* tr_ssha1_matches () */
     23#include "crypto-utils.h" /* tr_rand_buffer () */
    2324#include "fdlimit.h"
    2425#include "list.h"
     
    9293      unsigned char * buf = tr_new (unsigned char, n+1);
    9394
    94       tr_cryptoRandBuf (buf, n);
     95      tr_rand_buffer (buf, n);
    9596      for (i=0; i<n; ++i)
    9697        buf[i] = pool[ buf[i] % pool_size ];
  • trunk/libtransmission/session.c

    r14331 r14354  
    2929#include "blocklist.h"
    3030#include "cache.h"
    31 #include "crypto.h"
     31#include "crypto-utils.h"
    3232#include "fdlimit.h"
    3333#include "file.h"
     
    7979getRandomPort (tr_session * s)
    8080{
    81   return tr_cryptoWeakRandInt (s->randomPortHigh - s->randomPortLow + 1) + s->randomPortLow;
     81  return tr_rand_int_weak (s->randomPortHigh - s->randomPortLow + 1) + s->randomPortLow;
    8282}
    8383
     
    9797  memcpy (buf, PEERID_PREFIX, 8);
    9898
    99   tr_cryptoRandBuf (buf+8, 11);
     99  tr_rand_buffer (buf+8, 11);
    100100  for (i=8; i<19; ++i)
    101101    {
  • trunk/libtransmission/torrent.c

    r14347 r14354  
    3232#include "completion.h"
    3333#include "crypto.h" /* for tr_sha1 */
     34#include "crypto-utils.h"
    3435#include "error.h"
    3536#include "fdlimit.h" /* tr_fdTorrentClose */
     
    16491650  tr_torrentResetTransferStats (tor);
    16501651  tr_announcerTorrentStarted (tor);
    1651   tor->dhtAnnounceAt = now + tr_cryptoWeakRandInt (20);
    1652   tor->dhtAnnounce6At = now + tr_cryptoWeakRandInt (20);
     1652  tor->dhtAnnounceAt = now + tr_rand_int_weak (20);
     1653  tor->dhtAnnounce6At = now + tr_rand_int_weak (20);
    16531654  tor->lpdAnnounceAt = now;
    16541655  tr_peerMgrStartTorrent (tor);
  • trunk/libtransmission/tr-dht.c

    r14336 r14354  
    5151#include "transmission.h"
    5252#include "crypto.h"
     53#include "crypto-utils.h"
    5354#include "file.h"
    5455#include "log.h"
     
    9495{
    9596    const int roughly_msec = roughly_sec * 1000;
    96     const int msec = roughly_msec/2 + tr_cryptoWeakRandInt (roughly_msec);
     97    const int msec = roughly_msec/2 + tr_rand_int_weak (roughly_msec);
    9798    tr_wait_msec (msec);
    9899}
     
    308309         * so it should be something truly random. */
    309310        tr_logAddNamedInfo ("DHT", "Generating new id");
    310         tr_cryptoRandBuf (myid, 20);
     311        tr_rand_buffer (myid, 20);
    311312    }
    312313
     
    326327
    327328    dht_timer = evtimer_new (session->event_base, timer_callback, session);
    328     tr_timerAdd (dht_timer, 0, tr_cryptoWeakRandInt (1000000));
     329    tr_timerAdd (dht_timer, 0, tr_rand_int_weak (1000000));
    329330
    330331    tr_logAddNamedDbg ("DHT", "DHT initialized");
     
    610611
    611612            tor->dhtAnnounceAt = now + ((rc == 0)
    612                                      ? 5 + tr_cryptoWeakRandInt (5)
    613                                      : 25 * 60 + tr_cryptoWeakRandInt (3*60));
     613                                     ? 5 + tr_rand_int_weak (5)
     614                                     : 25 * 60 + tr_rand_int_weak (3*60));
    614615        }
    615616
     
    619620
    620621            tor->dhtAnnounce6At = now + ((rc == 0)
    621                                       ? 5 + tr_cryptoWeakRandInt (5)
    622                                       : 25 * 60 + tr_cryptoWeakRandInt (3*60));
     622                                      ? 5 + tr_rand_int_weak (5)
     623                                      : 25 * 60 + tr_rand_int_weak (3*60));
    623624        }
    624625    }
     
    653654    /* Being slightly late is fine,
    654655       and has the added benefit of adding some jitter. */
    655     tr_timerAdd (dht_timer, tosleep, tr_cryptoWeakRandInt (1000000));
     656    tr_timerAdd (dht_timer, tosleep, tr_rand_int_weak (1000000));
    656657}
    657658
     
    689690dht_random_bytes (void * buf, size_t size)
    690691{
    691     tr_cryptoRandBuf (buf, size);
     692    tr_rand_buffer (buf, size);
    692693    return size;
    693694}
  • trunk/libtransmission/tr-utp.c

    r14190 r14354  
    3232#include "net.h"
    3333#include "session.h"
    34 #include "crypto.h" /* tr_cryptoWeakRandInt () */
     34#include "crypto-utils.h" /* tr_rand_int_weak () */
    3535#include "peer-mgr.h"
    3636#include "tr-utp.h"
     
    144144    if (tr_sessionIsUTPEnabled (ss)) {
    145145        sec = 0;
    146         usec = UTP_INTERVAL_US / 2 + tr_cryptoWeakRandInt (UTP_INTERVAL_US);
     146        usec = UTP_INTERVAL_US / 2 + tr_rand_int_weak (UTP_INTERVAL_US);
    147147    } else {
    148148        /* If somebody has disabled uTP, then we still want to run
     
    152152           well use a large timeout. */
    153153        sec = 2;
    154         usec = tr_cryptoWeakRandInt (1000000);
     154        usec = tr_rand_int_weak (1000000);
    155155    }
    156156    tr_timerAdd (utp_timer, sec, usec);
  • trunk/libtransmission/utils-test.c

    r14336 r14354  
    2222#include "ConvertUTF.h" /* tr_utf8_validate*/
    2323#include "platform.h"
    24 #include "crypto.h"
     24#include "crypto-utils.h" /* tr_rand_int_weak */
    2525#include "utils.h"
    2626#include "web.h"
     
    244244  /* populate buf with random ints */
    245245  for (i=0; i<n; ++i)
    246     buf[i] = tr_cryptoWeakRandInt (range);
     246    buf[i] = tr_rand_int_weak (range);
    247247
    248248  /* find the best k */
     
    413413}
    414414
    415 static int
    416 test_cryptoRand (void)
    417 {
    418   int i;
    419 
    420   /* test that tr_cryptoRandInt () stays in-bounds */
    421   for (i = 0; i < 100000; ++i)
    422     {
    423       const int val = tr_cryptoRandInt (100);
    424        check (val >= 0);
    425        check (val < 100);
    426     }
    427 
    428   return 0;
    429 }
    430 
    431415static char *
    432416test_strdup_printf_valist (const char * fmt, ...)
     
    537521                             test_base64,
    538522                             test_buildpath,
    539                              test_cryptoRand,
    540523                             test_hex,
    541524                             test_lowerbound,
Note: See TracChangeset for help on using the changeset viewer.