Example #1
0
tr_bool
tr_ssha1_matches( const char * source, const char * pass )
{
    char * salt;
    size_t saltlen;
    char * hashed;
    uint8_t buf[SHA_DIGEST_LENGTH];
    tr_bool result;

    /* extract the salt */
    saltlen = strlen( source ) - 2*SHA_DIGEST_LENGTH-1;
    salt = tr_malloc( saltlen );
    memcpy( salt, source + 2*SHA_DIGEST_LENGTH+1, saltlen );

    /* hash pass + salt */
    hashed = tr_malloc( 2*SHA_DIGEST_LENGTH + saltlen + 2 );
    tr_sha1( buf, pass, strlen( pass ), salt, saltlen, NULL );
    tr_sha1_to_hex( &hashed[1], buf );
    memcpy( hashed + 1+2*SHA_DIGEST_LENGTH, salt, saltlen );
    hashed[1+2*SHA_DIGEST_LENGTH + saltlen] = '\0';
    hashed[0] = '{';

    result = strcmp( source, hashed ) == 0 ? TRUE : FALSE;

    tr_free( hashed );
    tr_free( salt );

    return result;
}
Example #2
0
/**************************************************************************
* Creates a database track record.
**************************************************************************/
TrackRecord* TrackRecord_create(int id, float c0, float c1)
{
  TrackRecord* p_rv = (TrackRecord*) tr_malloc();
  p_rv->id = id;
  p_rv->x = c0;
  p_rv->y = c1;
  return p_rv;
}
Example #3
0
bool
tr_dh_make_key (tr_dh_ctx_t   raw_handle,
                size_t        private_key_length UNUSED,
                uint8_t     * public_key,
                size_t      * public_key_length)
{
  struct tr_dh_ctx * handle = raw_handle;
  word32 my_private_key_length, my_public_key_length;
  tr_lock * rng_lock = get_rng_lock ();

  assert (handle != NULL);
  assert (public_key != NULL);

  if (handle->private_key == NULL)
    handle->private_key = tr_malloc (handle->key_length);

  tr_lockLock (rng_lock);

  if (!check_result (DhGenerateKeyPair (&handle->dh, get_rng (),
                                        handle->private_key, &my_private_key_length,
                                        public_key, &my_public_key_length)))
    {
      tr_lockUnlock (rng_lock);
      return false;
    }

  tr_lockUnlock (rng_lock);

  tr_dh_align_key (public_key, my_public_key_length, handle->key_length);

  handle->private_key_length = my_private_key_length;

  if (public_key_length != NULL)
    *public_key_length = handle->key_length;

  return true;
}
Example #4
0
void*
tr_memdup (const void * src, size_t byteCount)
{
  return memcpy (tr_malloc (byteCount), src, byteCount);
}
Example #5
0
static void
tr_watchdir_win32_on_event (struct bufferevent * event,
                            void               * context)
{
  const tr_watchdir_t handle = context;
  size_t nread;
  size_t name_size = MAX_PATH * sizeof (WCHAR);
  char * buffer = tr_malloc (sizeof (FILE_NOTIFY_INFORMATION) + name_size);
  PFILE_NOTIFY_INFORMATION ev = (PFILE_NOTIFY_INFORMATION) buffer;
  const size_t header_size = offsetof (FILE_NOTIFY_INFORMATION, FileName);

  /* Read the size of the struct excluding name into buf. Guaranteed to have at
     least sizeof (*ev) available */
  while ((nread = bufferevent_read (event, ev, header_size)) != 0)
    {
      if (nread == (size_t) -1)
        {
          log_error ("Failed to read event: %s", tr_strerror (errno));
          break;
        }

      if (nread != header_size)
        {
          log_error ("Failed to read event: expected %zu, got %zu bytes.",
                     header_size, nread);
          break;
        }

      const size_t nleft = ev->NextEntryOffset - nread;

      assert (ev->FileNameLength % sizeof (WCHAR) == 0);
      assert (ev->FileNameLength > 0);
      assert (ev->FileNameLength <= nleft);

      if (nleft > name_size)
        {
          name_size = nleft;
          buffer = tr_realloc (buffer, sizeof (FILE_NOTIFY_INFORMATION) + name_size);
          ev = (PFILE_NOTIFY_INFORMATION) buffer;
        }

      /* Consume entire name into buffer */
      if ((nread = bufferevent_read (event, buffer + header_size, nleft)) == (size_t) -1)
        {
          log_error ("Failed to read name: %s", tr_strerror (errno));
          break;
        }

      if (nread != nleft)
        {
          log_error ("Failed to read name: expected %zu, got %zu bytes.", nleft, nread);
          break;
        }

      if (ev->Action == FILE_ACTION_ADDED ||
          ev->Action == FILE_ACTION_MODIFIED ||
          ev->Action == FILE_ACTION_RENAMED_NEW_NAME)
        {
          char * name = tr_win32_native_to_utf8 (ev->FileName,
                                                 ev->FileNameLength / sizeof (WCHAR));
          if (name != NULL)
            {
              tr_watchdir_process (handle, name);
              tr_free (name);
            }
        }
    }

  tr_free (buffer);
}
static struct tr_dh_secret* tr_dh_secret_new(size_t key_length)
{
    struct tr_dh_secret* handle = tr_malloc(sizeof(struct tr_dh_secret) + key_length);
    handle->key_length = key_length;
    return handle;
}