Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
tg
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
tg
Commits
ea2dbbdf
Commit
ea2dbbdf
authored
Jan 21, 2014
by
vysheng
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #39 from antma/master
improve portability by removing rdtsc calls
parents
22a81ff8
192b82ef
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
24 deletions
+42
-24
mtproto-client.c
mtproto-client.c
+1
-2
mtproto-common.c
mtproto-common.c
+41
-16
mtproto-common.h
mtproto-common.h
+0
-6
No files found.
mtproto-client.c
View file @
ea2dbbdf
...
...
@@ -90,14 +90,13 @@ struct connection_methods auth_methods = {
};
long
long
precise_time
;
long
long
precise_time_rdtsc
;
double
get_utime
(
int
clock_id
)
{
struct
timespec
T
;
my_clock_gettime
(
clock_id
,
&
T
);
double
res
=
T
.
tv_sec
+
(
double
)
T
.
tv_nsec
*
1e-9
;
if
(
clock_id
==
CLOCK_REALTIME
)
{
precise_time
=
(
long
long
)
(
res
*
(
1LL
<<
32
));
precise_time_rdtsc
=
rdtsc
();
}
return
res
;
}
...
...
mtproto-common.c
View file @
ea2dbbdf
...
...
@@ -65,6 +65,8 @@ int get_random_bytes (unsigned char *buf, int n) {
if
(
verbosity
>=
3
)
{
logprintf
(
"added %d bytes of real entropy to secure random numbers seed
\n
"
,
r
);
}
}
else
{
r
=
0
;
}
close
(
h
);
}
...
...
@@ -76,13 +78,12 @@ int get_random_bytes (unsigned char *buf, int n) {
}
int
s
=
read
(
h
,
buf
+
r
,
n
-
r
);
close
(
h
);
if
(
s
<
0
)
{
r
eturn
r
;
if
(
s
>
0
)
{
r
+=
s
;
}
r
+=
s
;
}
if
(
r
>=
(
int
)
sizeof
(
long
))
{
if
(
r
>=
(
int
)
sizeof
(
long
))
{
*
(
long
*
)
buf
^=
lrand48
();
srand48
(
*
(
long
*
)
buf
);
}
...
...
@@ -105,38 +106,62 @@ void my_clock_gettime (int clock_id UU, struct timespec *T) {
#endif
}
/* RDTSC */
#if defined(__i386__)
#define HAVE_RDTSC
static
__inline__
unsigned
long
long
rdtsc
(
void
)
{
unsigned
long
long
int
x
;
__asm__
volatile
(
"rdtsc"
:
"=A"
(
x
));
return
x
;
}
#elif defined(__x86_64__)
#define HAVE_RDTSC
static
__inline__
unsigned
long
long
rdtsc
(
void
)
{
unsigned
hi
,
lo
;
__asm__
__volatile__
(
"rdtsc"
:
"=a"
(
lo
),
"=d"
(
hi
));
return
((
unsigned
long
long
)
lo
)
|
(((
unsigned
long
long
)
hi
)
<<
32
);
}
#endif
void
prng_seed
(
const
char
*
password_filename
,
int
password_length
)
{
unsigned
char
*
a
=
talloc0
(
64
+
password_length
);
long
long
r
=
rdtsc
();
struct
timespec
T
;
my_clock_gettime
(
CLOCK_REALTIME
,
&
T
);
memcpy
(
a
,
&
T
.
tv_sec
,
4
);
memcpy
(
a
+
4
,
&
T
.
tv_nsec
,
4
);
memcpy
(
a
+
8
,
&
r
,
8
);
RAND_add
(
&
T
,
sizeof
(
T
),
4
.
0
);
#ifdef HAVE_RDTSC
unsigned
long
long
r
=
rdtsc
();
RAND_add
(
&
r
,
8
,
4
.
0
);
#endif
unsigned
short
p
=
getpid
();
memcpy
(
a
+
16
,
&
p
,
2
);
int
s
=
get_random_bytes
(
a
+
18
,
32
)
+
18
;
if
(
password_filename
)
{
RAND_add
(
&
p
,
sizeof
(
p
),
0
.
0
);
p
=
getppid
();
RAND_add
(
&
p
,
sizeof
(
p
),
0
.
0
);
unsigned
char
rb
[
32
];
int
s
=
get_random_bytes
(
rb
,
32
);
if
(
s
>
0
)
{
RAND_add
(
rb
,
s
,
s
);
}
memset
(
rb
,
0
,
sizeof
(
rb
));
if
(
password_filename
&&
password_length
>
0
)
{
int
fd
=
open
(
password_filename
,
O_RDONLY
);
if
(
fd
<
0
)
{
logprintf
(
"Warning: fail to open password file -
\"
%s
\"
, %m.
\n
"
,
password_filename
);
}
else
{
int
l
=
read
(
fd
,
a
+
s
,
password_length
);
unsigned
char
*
a
=
talloc0
(
password_length
);
int
l
=
read
(
fd
,
a
,
password_length
);
if
(
l
<
0
)
{
logprintf
(
"Warning: fail to read password file -
\"
%s
\"
, %m.
\n
"
,
password_filename
);
}
else
{
if
(
verbosity
>
0
)
{
logprintf
(
"read %d bytes from password file.
\n
"
,
l
);
}
s
+=
l
;
RAND_add
(
a
,
l
,
l
)
;
}
close
(
fd
);
tfree_secure
(
a
,
password_length
);
}
}
RAND_seed
(
a
,
s
);
BN_ctx
=
BN_CTX_new
();
tfree_secure
(
a
,
64
+
password_length
);
ensure_ptr
(
BN_ctx
);
}
int
serialize_bignum
(
BIGNUM
*
b
,
char
*
buffer
,
int
maxlen
)
{
...
...
mtproto-common.h
View file @
ea2dbbdf
...
...
@@ -375,12 +375,6 @@ static inline void fetch_skip_str (void) {
fetch_str
(
l
);
}
static
__inline__
unsigned
long
long
rdtsc
(
void
)
{
unsigned
hi
,
lo
;
__asm__
__volatile__
(
"rdtsc"
:
"=a"
(
lo
),
"=d"
(
hi
));
return
(
(
unsigned
long
long
)
lo
)
|
(
((
unsigned
long
long
)
hi
)
<<
32
);
}
static
inline
long
have_prefetch_ints
(
void
)
{
return
in_end
-
in_ptr
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment