lilium-voyager/code/autoupdater/rsa_tools/rsa_sign.c
Ryan C. Gordon ece37f1390 Initial add of rsa_tools.
This is just a simple RSA public key digital signature thing built on
libtomcrypt. The gist:

Some admin will generate a public/private key with rsa_make_keys, keeping the
private key secret. Using the private key and rsa_sign, the admin will sign
the autoupdater manifests, generating manifest.txt.sig.

The public key ships with the game (adding 270 bytes to the download), the
.sig is downloaded with the manifest by the autoupdater (256 bytes extra
download), then the autoupdater checks the manifest against the signature
with the public key. if the signature isn't valid (the manifest was tampered
with or corrupt), the autoupdater refuses to continue.

If the manifest is to be trusted, it lists sha256 checksums for every file to
download, so there's no need to sign every file; if they can't tamper with the
manifest, they can't tamper with any other file to be updated since the file's
listed sha256 won't match.

If the private key is compromised, we generate new keys and ship new
installers, so new installations will be able to update but existing ones
will need to do a new install to keep getting updates. Don't let the private
key get compromised. The private key doesn't go on a public server. Maybe it
doesn't even live on the admin's laptop hard drive.

If the download server is compromised and serving malware, the autoupdater
will reject it outright if they haven't compromised the private key, generated
a new manifest, and signed it with the private key.

libtomcrypt is sort of a big pile of source code, so instead of putting it
in revision control, we have a script to download it. Most things don't need
it. It lives on GitHub, so we _could_ do a git submodule, but most people
don't need it, so why waste their disk and bandwidth? That said, when compiled
you end up with a few hundred kilobytes of binary code to verify a signature
and no external dependencies, so it seems like a win.
2017-06-03 20:26:07 -04:00

75 lines
2 KiB
C

#include "rsa_common.h"
static void sign_file(const char *fname, rsa_key *key, prng_state *prng, const int prng_index, const int hash_index)
{
const size_t sigfnamelen = strlen(fname) + 5;
char *sigfname = (char *) malloc(sigfnamelen);
unsigned char hash[256];
unsigned long hashlen = sizeof (hash);
unsigned char sig[1024];
unsigned long siglen = sizeof (sig);
int rc = 0;
int status = 0;
if (!sigfname) {
fail("out of memory");
}
if ((rc = hash_file(hash_index, fname, hash, &hashlen)) != CRYPT_OK) {
fail("hash_file for '%s' failed: %s", fname, error_to_string(rc));
}
if ((rc = rsa_sign_hash(hash, hashlen, sig, &siglen, prng, prng_index, hash_index, SALT_LEN, key)) != CRYPT_OK) {
fail("rsa_sign_hash for '%s' failed: %s", fname, error_to_string(rc));
}
if ((rc = rsa_verify_hash(sig, siglen, hash, hashlen, hash_index, SALT_LEN, &status, key)) != CRYPT_OK) {
fail("rsa_verify_hash for '%s' failed: %s", fname, error_to_string(rc));
}
if (!status) {
fail("Generated signature isn't valid! Bug in the program!");
}
snprintf(sigfname, sigfnamelen, "%s.sig", fname);
write_file(sigfname, sig, siglen);
free(sigfname);
}
int main(int argc, char **argv)
{
int rc = 0;
prng_state prng;
int prng_index, hash_index;
rsa_key key;
int i;
ltc_mp = tfm_desc;
prng_index = register_prng(&sprng_desc); /* (fortuna_desc is a good choice if your platform's PRNG sucks.) */
if (prng_index == -1) {
fail("Failed to register a RNG");
}
hash_index = register_hash(&sha256_desc);
if (hash_index == -1) {
fail("Failed to register sha256 hasher");
}
if ((rc = rng_make_prng(128, prng_index, &prng, NULL)) != CRYPT_OK) {
fail("rng_make_prng failed: %s", error_to_string(rc));
}
read_rsakey(&key, "privatekey.bin");
for (i = 1; i < argc; i++) {
sign_file(argv[i], &key, &prng, prng_index, hash_index);
}
rsa_free(&key);
return 0;
}
/* end of rsa_sign.c ... */