Commit ee46a524 authored by Stefan Westerfeld's avatar Stefan Westerfeld

Support loading watermarking key from file (using --key <file>).

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent b582c712
key:
- --key loading
- multiple values per block
- avoid reroll in up_down generation
- move shuffle function to Random class
......
......@@ -65,6 +65,7 @@ print_usage()
printf (" --water-delta set watermarking delta [%.4f]\n", Params::water_delta);
printf (" --pre-scale set scaling used for normalization [%.3f]\n", Params::pre_scale);
printf (" --linear disable non-linear bit storage\n");
printf (" --key <file> load watermarking key from file\n");
}
static bool
......@@ -166,6 +167,11 @@ parse_options (int *argc_p,
Params::have_key++;
Random::set_global_test_key (atoi (opt_arg));
}
else if (check_arg (argc, argv, &i, "--key", &opt_arg))
{
Params::have_key++;
Random::load_global_key (opt_arg);
}
}
/* resort argc/argv */
......
#include "random.hh"
#include "utils.hh"
#include <regex>
using std::string;
using std::vector;
using std::regex;
using std::regex_match;
static std::vector<unsigned char> aes_key (16);
static std::vector<unsigned char> aes_key (16); // 128 bits
static constexpr auto GCRY_CIPHER = GCRY_CIPHER_AES128;
static void
......@@ -126,18 +132,68 @@ Random::set_global_test_key (uint64_t key)
uint64_to_buffer (key, &aes_key[0]);
}
std::string
Random::gen_key()
void
Random::load_global_key (const string& key_file)
{
unsigned char key[16];
gcry_randomize (key, 16, /* long term key material strength */ GCRY_VERY_STRONG_RANDOM);
string s;
for (auto k : key)
FILE *f = fopen (key_file.c_str(), "r");
if (!f)
{
char buffer[256];
fprintf (stderr, "audiowmark: error opening key file: '%s'\n", key_file.c_str());
exit (1);
}
sprintf (buffer, "%02x", k);
s += buffer;
const regex blank_re (R"(\s*(#.*)?[\r\n]+)");
const regex key_re (R"(\s*key\s+([0-9a-f]+)\s*(#.*)?[\r\n]+)");
char buffer[1024];
int line = 1;
int keys = 0;
while (fgets (buffer, 1024, f))
{
string s = buffer;
std::smatch match;
if (regex_match (s, blank_re))
{
/* blank line or comment */
}
else if (regex_match (s, match, key_re))
{
/* line containing aes key */
vector<unsigned char> key = hex_str_to_vec (match[1].str());
if (key.size() != aes_key.size())
{
fprintf (stderr, "audiowmark: wrong key length in key file '%s', line %d\n => required key length is %zd bits\n", key_file.c_str(), line, aes_key.size() * 8);
exit (1);
}
aes_key = key;
keys++;
}
else
{
fprintf (stderr, "audiowmark: parse error in key file '%s', line %d\n", key_file.c_str(), line);
exit (1);
}
line++;
}
return s;
fclose (f);
if (keys > 1)
{
fprintf (stderr, "audiowmark: key file '%s' contains more than one key\n", key_file.c_str());
exit (1);
}
if (keys == 0)
{
fprintf (stderr, "audiowmark: key file '%s' contains no key\n", key_file.c_str());
exit (1);
}
}
std::string
Random::gen_key()
{
vector<unsigned char> key (16);
gcry_randomize (&key[0], 16, /* long term key material strength */ GCRY_VERY_STRONG_RANDOM);
return vec_to_hex_str (key);
}
......@@ -35,6 +35,7 @@ public:
uint64_t operator()();
static void set_global_test_key (uint64_t seed);
static void load_global_key (const std::string& key_file);
static std::string gen_key();
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment