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
192b82ef
Commit
192b82ef
authored
Jan 17, 2014
by
antma
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use rdtsc for seeding prng only at i386 and x86_64 platforms
https://github.com/vysheng/tg/issues/8
parent
22c1a411
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
17 deletions
+42
-17
mtproto-common.c
mtproto-common.c
+42
-17
No files found.
mtproto-common.c
View file @
192b82ef
...
...
@@ -65,11 +65,10 @@ 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
);
}
}
close
(
h
);
if
(
r
<
0
)
{
}
else
{
r
=
0
;
}
close
(
h
);
}
if
(
r
<
n
)
{
...
...
@@ -79,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
);
}
...
...
@@ -108,35 +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
);
struct
timespec
T
;
my_clock_gettime
(
CLOCK_REALTIME
,
&
T
);
memcpy
(
a
,
&
T
.
tv_sec
,
4
);
memcpy
(
a
+
4
,
&
T
.
tv_nsec
,
4
);
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
+
8
,
&
p
,
2
);
int
s
=
get_random_bytes
(
a
+
10
,
32
)
+
10
;
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
)
{
...
...
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