Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
audiowmark
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Stefan Westerfeld
audiowmark
Commits
261aedce
Commit
261aedce
authored
Nov 17, 2019
by
Stefan Westerfeld
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Provide re-seeding operation for random generator.
Signed-off-by:
Stefan Westerfeld
<
stefan@space.twc.de
>
parent
ca71ac9f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
32 deletions
+25
-32
random.cc
src/random.cc
+22
-29
random.hh
src/random.hh
+3
-3
No files found.
src/random.cc
View file @
261aedce
...
...
@@ -78,57 +78,50 @@ print (const string& label, const vector<unsigned char>& data)
}
#endif
Random
::
Random
(
uint64_t
seed
,
Stream
stream
)
Random
::
Random
(
uint64_t
s
tart_s
eed
,
Stream
stream
)
{
gcrypt_init
();
vector
<
unsigned
char
>
ctr
=
get_start_counter
(
seed
,
stream
);
// print ("CTR", ctr);
gcry_error_t
gcry_ret
=
gcry_cipher_open
(
&
aes_ctr_cipher
,
GCRY_CIPHER
,
GCRY_CIPHER_MODE_CTR
,
0
);
die_on_error
(
"gcry_cipher_open"
,
gcry_ret
);
gcry_ret
=
gcry_cipher_setkey
(
aes_ctr_cipher
,
&
aes_key
[
0
],
aes_key
.
size
());
die_on_error
(
"gcry_cipher_setkey"
,
gcry_ret
);
gcry_ret
=
gcry_cipher_setctr
(
aes_ctr_cipher
,
&
ctr
[
0
],
ctr
.
size
());
die_on_error
(
"gcry_cipher_setctr"
,
gcry_ret
);
}
gcry_ret
=
gcry_cipher_open
(
&
seed_cipher
,
GCRY_CIPHER
,
GCRY_CIPHER_MODE_ECB
,
0
);
die_on_error
(
"gcry_cipher_open"
,
gcry_ret
);
Random
::~
Random
()
{
gcry_cipher_close
(
aes_ctr_cipher
);
gcry_ret
=
gcry_cipher_setkey
(
seed_cipher
,
&
aes_key
[
0
],
aes_key
.
size
());
die_on_error
(
"gcry_cipher_setkey"
,
gcry_ret
);
seed
(
start_seed
,
stream
);
}
v
ector
<
unsigned
char
>
Random
::
get_start_counter
(
uint64_t
seed
,
Stream
stream
)
v
oid
Random
::
seed
(
uint64_t
seed
,
Stream
stream
)
{
gcry_error_t
gcry_ret
;
gcry_cipher_hd_t
cipher_hd
;
gcry_ret
=
gcry_cipher_open
(
&
cipher_hd
,
GCRY_CIPHER
,
GCRY_CIPHER_MODE_ECB
,
0
);
die_on_error
(
"gcry_cipher_open"
,
gcry_ret
);
gcry_ret
=
gcry_cipher_setkey
(
cipher_hd
,
&
aes_key
[
0
],
aes_key
.
size
());
die_on_error
(
"gcry_cipher_setkey"
,
gcry_ret
);
buffer_pos
=
0
;
buffer
.
clear
();
vector
<
unsigned
char
>
cipher_text
(
16
)
;
vector
<
unsigned
char
>
plain_text
(
16
)
;
unsigned
char
plain_text
[
aes_key
.
size
()]
=
{
0
,
}
;
unsigned
char
cipher_text
[
aes_key
.
size
()]
;
uint64_to_buffer
(
seed
,
&
plain_text
[
0
]);
plain_text
[
8
]
=
uint8_t
(
stream
);
// print ("SEED", plain_text);
gcry_ret
=
gcry_cipher_encrypt
(
cipher_hd
,
&
cipher_text
[
0
],
cipher_text
.
size
(),
&
plain_text
[
0
],
plain_text
.
size
());
gcry_error_t
gcry_ret
=
gcry_cipher_encrypt
(
seed_cipher
,
&
cipher_text
[
0
],
aes_key
.
size
(),
&
plain_text
[
0
],
aes_key
.
size
());
die_on_error
(
"gcry_cipher_encrypt"
,
gcry_ret
);
gcry_cipher_close
(
cipher_hd
);
gcry_ret
=
gcry_cipher_setctr
(
aes_ctr_cipher
,
&
cipher_text
[
0
],
aes_key
.
size
());
die_on_error
(
"gcry_cipher_setctr"
,
gcry_ret
);
}
return
cipher_text
;
Random
::~
Random
()
{
gcry_cipher_close
(
aes_ctr_cipher
);
gcry_cipher_close
(
seed_cipher
);
}
void
...
...
src/random.hh
View file @
261aedce
...
...
@@ -19,12 +19,11 @@ public:
frame_position
=
6
};
private
:
gcry_cipher_hd_t
aes_ctr_cipher
;
gcry_cipher_hd_t
aes_ctr_cipher
=
nullptr
;
gcry_cipher_hd_t
seed_cipher
=
nullptr
;
std
::
vector
<
uint64_t
>
buffer
;
size_t
buffer_pos
=
0
;
std
::
vector
<
unsigned
char
>
get_start_counter
(
uint64_t
seed
,
Stream
stream
);
void
die_on_error
(
const
char
*
func
,
gcry_error_t
error
);
public
:
Random
(
uint64_t
seed
,
Stream
stream
);
...
...
@@ -39,6 +38,7 @@ public:
return
buffer
[
buffer_pos
++
];
}
void
refill_buffer
();
void
seed
(
uint64_t
seed
,
Stream
stream
);
template
<
class
T
>
void
shuffle
(
std
::
vector
<
T
>&
result
)
...
...
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