Changeset 14354
- Timestamp:
- Dec 4, 2014, 11:27:38 AM (8 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 3 added
- 20 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/Makefile.am
r14332 r14354 29 29 ConvertUTF.c \ 30 30 crypto.c \ 31 crypto-utils.c \ 32 crypto-utils-openssl.c \ 31 33 error.c \ 32 34 fdlimit.c \ … … 90 92 ConvertUTF.h \ 91 93 crypto.h \ 94 crypto-utils.h \ 92 95 completion.h \ 93 96 error.h \ -
trunk/libtransmission/announcer-udp.c
r14241 r14354 20 20 #include "announcer.h" 21 21 #include "announcer-common.h" 22 #include "crypto .h" /* tr_cryptoRandBuf() */22 #include "crypto-utils.h" /* tr_rand_buffer () */ 23 23 #include "log.h" 24 24 #include "peer-io.h" … … 109 109 { 110 110 tau_transaction_t tmp; 111 tr_ cryptoRandBuf(&tmp, sizeof (tau_transaction_t));111 tr_rand_buffer (&tmp, sizeof (tau_transaction_t)); 112 112 return tmp; 113 113 } -
trunk/libtransmission/announcer.c
r14241 r14354 22 22 #include "announcer.h" 23 23 #include "announcer-common.h" 24 #include "crypto .h" /* tr_cryptoRandInt (), tr_cryptoWeakRandInt() */24 #include "crypto-utils.h" /* tr_rand_int (), tr_rand_int_weak () */ 25 25 #include "log.h" 26 26 #include "peer-mgr.h" /* tr_peerMgrCompactToPex () */ … … 168 168 a = tr_new0 (tr_announcer, 1); 169 169 a->stops = TR_PTR_ARRAY_INIT; 170 a->key = tr_ cryptoRandInt (INT_MAX);170 a->key = tr_rand_int (INT_MAX); 171 171 a->session = session; 172 172 a->slotsAvailable = MAX_CONCURRENT_TASKS; … … 350 350 tier->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC; 351 351 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)); 353 353 tier->tor = tor; 354 354 } … … 964 964 case 0: return 0; 965 965 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); 971 971 } 972 972 } -
trunk/libtransmission/bandwidth.c
r14241 r14354 13 13 #include "transmission.h" 14 14 #include "bandwidth.h" 15 #include "crypto .h" /* tr_cryptoWeakRandInt() */15 #include "crypto-utils.h" /* tr_rand_int_weak () */ 16 16 #include "log.h" 17 17 #include "peer-io.h" … … 211 211 while (n > 0) 212 212 { 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 */ 214 214 215 215 /* value of 3000 bytes chosen so that when using uTP we'll send a full-size -
trunk/libtransmission/bitfield-test.c
r14241 r14354 10 10 #include <string.h> /* strlen () */ 11 11 #include "transmission.h" 12 #include "crypto .h"12 #include "crypto-utils.h" 13 13 #include "bitfield.h" 14 14 #include "utils.h" /* tr_free */ … … 25 25 int count1; 26 26 int count2; 27 const int bitCount = 100 + tr_ cryptoWeakRandInt(1000);27 const int bitCount = 100 + tr_rand_int_weak (1000); 28 28 tr_bitfield bf; 29 29 30 30 /* generate a random bitfield */ 31 31 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); 35 35 do 36 end = tr_ cryptoWeakRandInt(bitCount);36 end = tr_rand_int_weak (bitCount); 37 37 while (end == begin); 38 38 -
trunk/libtransmission/crypto-test.c
r14282 r14354 12 12 #include "transmission.h" 13 13 #include "crypto.h" 14 #include "crypto-utils.h" 14 15 15 16 #include "libtransmission-test.h" … … 167 168 } 168 169 170 static int 171 test_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 169 186 int 170 187 main (void) … … 173 190 test_encrypt_decrypt, 174 191 test_sha1, 175 test_ssha1 }; 192 test_ssha1, 193 test_random }; 176 194 177 195 return runTests (tests, NUM_TESTS (tests)); -
trunk/libtransmission/crypto.c
r14347 r14354 9 9 10 10 #include <assert.h> 11 #include <inttypes.h> /* uint8_t */12 11 #include <stdarg.h> 13 #include <stdlib.h> /* abs () */14 12 #include <string.h> /* memcpy (), memset (), strcmp () */ 15 13 … … 19 17 #include <openssl/rc4.h> 20 18 #include <openssl/sha.h> 21 #include <openssl/rand.h>22 19 23 20 #include "transmission.h" 24 21 #include "crypto.h" 22 #include "crypto-utils.h" 25 23 #include "log.h" 26 24 #include "utils.h" … … 295 293 } 296 294 297 int298 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 int318 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 void334 tr_cryptoRandBuf (void * buf, size_t len)335 {336 if (RAND_pseudo_bytes ((unsigned char*)buf, len) != 1)337 logErrorFromSSL ();338 }339 340 295 /*** 341 296 **** … … 357 312 char buf[2*SHA_DIGEST_LENGTH + saltval_len + 2]; 358 313 359 tr_ cryptoRandBuf(salt, saltval_len);314 tr_rand_buffer (salt, saltval_len); 360 315 for (i=0; i<saltval_len; ++i) 361 316 salt[i] = salter[ salt[i] % salter_len ]; -
trunk/libtransmission/crypto.h
r14347 r14354 95 95 96 96 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 never104 * 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 112 97 /** @brief generate a SSHA password from its plaintext source */ 113 98 char* tr_ssha1 (const void * plaintext); -
trunk/libtransmission/file-win32.c
r14330 r14354 15 15 16 16 #include "transmission.h" 17 #include "crypto .h" /* tr_cryptoRandInt () */17 #include "crypto-utils.h" /* tr_rand_int () */ 18 18 #include "file.h" 19 19 #include "utils.h" … … 206 206 while (i > 0 && path_template[i - 1] == 'X') 207 207 { 208 const int c = tr_ cryptoRandInt (26 + 26 + 10);208 const int c = tr_rand_int (26 + 26 + 10); 209 209 path[i - 1] = c < 26 ? c + 'A' : (c < 26 + 26 ? (c - 26) + 'a' : (c - 26 - 26) + '0'); 210 210 --i; -
trunk/libtransmission/handshake.c
r14347 r14354 17 17 #include "transmission.h" 18 18 #include "clients.h" 19 #include "crypto .h"19 #include "crypto-utils.h" 20 20 #include "handshake.h" 21 21 #include "log.h" … … 325 325 326 326 /* 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); 329 329 walk += len; 330 330 … … 749 749 memcpy (walk, myKey, len); 750 750 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); 753 753 walk += len; 754 754 -
trunk/libtransmission/makemeta-test.c
r14331 r14354 11 11 12 12 #include "transmission.h" 13 #include "crypto .h"13 #include "crypto-utils.h" 14 14 #include "file.h" 15 15 #include "makemeta.h" … … 230 230 231 231 /* build random payloads */ 232 payloadCount = 1 + tr_ cryptoWeakRandInt(maxFileCount);232 payloadCount = 1 + tr_rand_int_weak (maxFileCount); 233 233 payloads = tr_new0 (void*, payloadCount); 234 234 payloadSizes = tr_new0 (size_t, payloadCount); 235 235 for (i=0; i<payloadCount; i++) 236 236 { 237 const size_t n = 1 + tr_ cryptoWeakRandInt(maxFileSize);237 const size_t n = 1 + tr_rand_int_weak (maxFileSize); 238 238 payloads[i] = tr_new (char, n); 239 tr_ cryptoRandBuf(payloads[i], n);239 tr_rand_buffer (payloads[i], n); 240 240 payloadSizes[i] = n; 241 241 } -
trunk/libtransmission/peer-io.c
r14347 r14354 21 21 #include "session.h" 22 22 #include "bandwidth.h" 23 #include "crypto.h"24 23 #include "log.h" 25 24 #include "net.h" -
trunk/libtransmission/peer-mgr.c
r14347 r14354 25 25 #include "clients.h" 26 26 #include "completion.h" 27 #include "crypto .h"27 #include "crypto-utils.h" 28 28 #include "handshake.h" 29 29 #include "log.h" … … 1098 1098 piece->index = pool[i]; 1099 1099 piece->requestCount = 0; 1100 piece->salt = tr_ cryptoWeakRandInt(4096);1100 piece->salt = tr_rand_int_weak (4096); 1101 1101 } 1102 1102 … … 1864 1864 if (a == NULL) 1865 1865 { 1866 const int jitter = tr_ cryptoWeakRandInt(60*10);1866 const int jitter = tr_rand_int_weak (60*10); 1867 1867 a = tr_new0 (struct peer_atom, 1); 1868 1868 a->addr = *addr; … … 2949 2949 rechoke[rechoke_count].peer = peer; 2950 2950 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); 2952 2952 rechoke_count++; 2953 2953 } … … 3089 3089 n->wasChoked = tr_peerMsgsIsPeerChoked (msgs); 3090 3090 n->rate = getRate (s->tor, atom, now); 3091 n->salt = tr_ cryptoWeakRandInt(INT_MAX);3091 n->salt = tr_rand_int_weak (INT_MAX); 3092 3092 n->isChoked = true; 3093 3093 } … … 3140 3140 if ((n = tr_ptrArraySize (&randPool))) 3141 3141 { 3142 c = tr_ptrArrayNth (&randPool, tr_ cryptoWeakRandInt(n));3142 c = tr_ptrArrayNth (&randPool, tr_rand_int_weak (n)); 3143 3143 c->isChoked = false; 3144 3144 s->optimistic = c->msgs; … … 3983 3983 if (isPeerCandidate (tor, atom, now)) 3984 3984 { 3985 const uint8_t salt = tr_ cryptoWeakRandInt(1024);3985 const uint8_t salt = tr_rand_int_weak (1024); 3986 3986 walk->tor = tor; 3987 3987 walk->atom = atom; -
trunk/libtransmission/peer-msgs.c
r14347 r14354 21 21 #include "cache.h" 22 22 #include "completion.h" 23 #include "crypto.h" /* tr_sha1 () */24 23 #include "file.h" 25 24 #include "log.h" -
trunk/libtransmission/rpc-server.c
r14327 r14354 20 20 21 21 #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 () */ 23 24 #include "fdlimit.h" 24 25 #include "list.h" … … 92 93 unsigned char * buf = tr_new (unsigned char, n+1); 93 94 94 tr_ cryptoRandBuf(buf, n);95 tr_rand_buffer (buf, n); 95 96 for (i=0; i<n; ++i) 96 97 buf[i] = pool[ buf[i] % pool_size ]; -
trunk/libtransmission/session.c
r14331 r14354 29 29 #include "blocklist.h" 30 30 #include "cache.h" 31 #include "crypto .h"31 #include "crypto-utils.h" 32 32 #include "fdlimit.h" 33 33 #include "file.h" … … 79 79 getRandomPort (tr_session * s) 80 80 { 81 return tr_ cryptoWeakRandInt(s->randomPortHigh - s->randomPortLow + 1) + s->randomPortLow;81 return tr_rand_int_weak (s->randomPortHigh - s->randomPortLow + 1) + s->randomPortLow; 82 82 } 83 83 … … 97 97 memcpy (buf, PEERID_PREFIX, 8); 98 98 99 tr_ cryptoRandBuf(buf+8, 11);99 tr_rand_buffer (buf+8, 11); 100 100 for (i=8; i<19; ++i) 101 101 { -
trunk/libtransmission/torrent.c
r14347 r14354 32 32 #include "completion.h" 33 33 #include "crypto.h" /* for tr_sha1 */ 34 #include "crypto-utils.h" 34 35 #include "error.h" 35 36 #include "fdlimit.h" /* tr_fdTorrentClose */ … … 1649 1650 tr_torrentResetTransferStats (tor); 1650 1651 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); 1653 1654 tor->lpdAnnounceAt = now; 1654 1655 tr_peerMgrStartTorrent (tor); -
trunk/libtransmission/tr-dht.c
r14336 r14354 51 51 #include "transmission.h" 52 52 #include "crypto.h" 53 #include "crypto-utils.h" 53 54 #include "file.h" 54 55 #include "log.h" … … 94 95 { 95 96 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); 97 98 tr_wait_msec (msec); 98 99 } … … 308 309 * so it should be something truly random. */ 309 310 tr_logAddNamedInfo ("DHT", "Generating new id"); 310 tr_ cryptoRandBuf(myid, 20);311 tr_rand_buffer (myid, 20); 311 312 } 312 313 … … 326 327 327 328 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)); 329 330 330 331 tr_logAddNamedDbg ("DHT", "DHT initialized"); … … 610 611 611 612 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)); 614 615 } 615 616 … … 619 620 620 621 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)); 623 624 } 624 625 } … … 653 654 /* Being slightly late is fine, 654 655 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)); 656 657 } 657 658 … … 689 690 dht_random_bytes (void * buf, size_t size) 690 691 { 691 tr_ cryptoRandBuf(buf, size);692 tr_rand_buffer (buf, size); 692 693 return size; 693 694 } -
trunk/libtransmission/tr-utp.c
r14190 r14354 32 32 #include "net.h" 33 33 #include "session.h" 34 #include "crypto .h" /* tr_cryptoWeakRandInt() */34 #include "crypto-utils.h" /* tr_rand_int_weak () */ 35 35 #include "peer-mgr.h" 36 36 #include "tr-utp.h" … … 144 144 if (tr_sessionIsUTPEnabled (ss)) { 145 145 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); 147 147 } else { 148 148 /* If somebody has disabled uTP, then we still want to run … … 152 152 well use a large timeout. */ 153 153 sec = 2; 154 usec = tr_ cryptoWeakRandInt(1000000);154 usec = tr_rand_int_weak (1000000); 155 155 } 156 156 tr_timerAdd (utp_timer, sec, usec); -
trunk/libtransmission/utils-test.c
r14336 r14354 22 22 #include "ConvertUTF.h" /* tr_utf8_validate*/ 23 23 #include "platform.h" 24 #include "crypto .h"24 #include "crypto-utils.h" /* tr_rand_int_weak */ 25 25 #include "utils.h" 26 26 #include "web.h" … … 244 244 /* populate buf with random ints */ 245 245 for (i=0; i<n; ++i) 246 buf[i] = tr_ cryptoWeakRandInt(range);246 buf[i] = tr_rand_int_weak (range); 247 247 248 248 /* find the best k */ … … 413 413 } 414 414 415 static int416 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 431 415 static char * 432 416 test_strdup_printf_valist (const char * fmt, ...) … … 537 521 test_base64, 538 522 test_buildpath, 539 test_cryptoRand,540 523 test_hex, 541 524 test_lowerbound,
Note: See TracChangeset
for help on using the changeset viewer.