tomato/other/fun/sign.c
Green Sky cae0ab9c5c Squashed 'external/toxcore/c-toxcore/' changes from 03e9fbf3703..55752a2e2ef
55752a2e2ef fix(toxav): pass video bit rate as kbit Previously we unintentionally made it Mbit.
7e573280a75 docs(toxav): fix docs of toxav.h - fix units to be more readable - use width before height consistently - video -> audio typo
5f88a084e8c fix: friend_connections leak on allocation failure clean up when it only contains connections in the NONE state
6d27a1ae178 fix: wrong comment for closelist
ce4f29e8036 cleanup: Fix all `-Wsign-compare` warnings.
4d4251c397f chore: lower cirrus ci timeout drastically
40676284507 fix: events leak that can occur if allocation fails rare in practice, found by fuzzing
9610ac31c5f fix: Return an error instead of crashing on nullptr args in NGC.
a57c2c8f956 refactor: Make ToxAV independent of toxcore internals.
5752fc29f86 refactor: Make tox-bootstrapd use bool instead of int
df675786eb2 chore: Add release-drafter github action.
03fd7a69dcf chore: Use toktok's cmp instead of upstream.
350c0ba1205 cleanup: Sort apk/apt install commands in Dockerfiles.
8c1bda502cb chore(deps): bump golang.org/x/net
ddb9d3210da chore: Upgrade to FreeBSD 14.1 in cirrus build.
e9076f45bd3 chore(cmake): set options changes as cache and with force

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 55752a2e2ef894bfa6d7a2a21a0278e3f2bede7d
2024-11-09 13:44:30 +01:00

156 lines
4.0 KiB
C

/* Binary signer/checker using ed25519
*
* Compile with:
* gcc -o sign sign.c -lsodium
*
* Generate a keypair:
* ./sign g
*
* Sign a file:
* ./sign s PRIVATEKEY file.bin signedfile.bin
*
* Check a file:
*
* ./sign c PUBKEY signedfile.bin
*
* NOTE: The signature is appended to the end of the file.
*/
#include <sodium.h>
#include <string.h>
#include "../../toxcore/ccompat.h"
static int load_file(const char *filename, unsigned char **result)
{
int size = 0;
FILE *f = fopen(filename, "rb");
if (f == nullptr) {
*result = nullptr;
return -1; // -1 means file opening fail
}
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
*result = (unsigned char *)malloc(size + 1);
if ((size_t)size != fread(*result, sizeof(char), size, f)) {
free(*result);
fclose(f);
return -2; // -2 means file reading fail
}
fclose(f);
(*result)[size] = 0;
return size;
}
int main(int argc, char *argv[])
{
unsigned char pk[crypto_sign_ed25519_PUBLICKEYBYTES];
unsigned char sk[crypto_sign_ed25519_SECRETKEYBYTES];
if (argc == 2 && argv[1][0] == 'g') {
crypto_sign_ed25519_keypair(pk, sk);
printf("Public key:\n");
for (uint32_t i = 0; i < crypto_sign_ed25519_PUBLICKEYBYTES; ++i) {
printf("%02X", pk[i]);
}
printf("\nSecret key:\n");
for (uint32_t i = 0; i < crypto_sign_ed25519_SECRETKEYBYTES; ++i) {
printf("%02X", sk[i]);
}
printf("\n");
}
if (argc == 5 && argv[1][0] == 's') {
const char *hex_end = nullptr;
if (sodium_hex2bin(sk, sizeof(sk), argv[2], strlen(argv[2]), nullptr, nullptr, &hex_end) != 0
|| hex_end != argv[2] + strlen(argv[2])) {
printf("Invalid secret key provided.\n");
goto fail;
}
unsigned char *data = nullptr;
int size = load_file(argv[3], &data);
if (size < 0) {
goto fail;
}
unsigned long long smlen;
unsigned char *sm = (unsigned char *)malloc(size + crypto_sign_ed25519_BYTES * 2);
crypto_sign_ed25519(sm, &smlen, data, size, sk);
free(data);
if (smlen - size != crypto_sign_ed25519_BYTES) {
free(sm);
goto fail;
}
FILE *f = fopen(argv[4], "wb");
if (f == nullptr) {
free(sm);
goto fail;
}
memcpy(sm + smlen, sm, crypto_sign_ed25519_BYTES); // Move signature from beginning to end of file.
if (fwrite(sm + (smlen - size), 1, smlen, f) != smlen) {
fclose(f);
free(sm);
goto fail;
}
fclose(f);
free(sm);
printf("Signed successfully.\n");
}
if (argc == 4 && argv[1][0] == 'c') {
const char *hex_end = nullptr;
if (sodium_hex2bin(pk, sizeof(pk), argv[2], strlen(argv[2]), nullptr, nullptr, &hex_end) != 0
|| hex_end != argv[2] + strlen(argv[2])) {
printf("Invalid public key provided.\n");
goto fail;
}
unsigned char *data = nullptr;
int size = load_file(argv[3], &data);
if (size < 0) {
goto fail;
}
unsigned char *signe = (unsigned char *)malloc(size + crypto_sign_ed25519_BYTES);
memcpy(signe, data + size - crypto_sign_ed25519_BYTES,
crypto_sign_ed25519_BYTES); // Move signature from end to beginning of file.
memcpy(signe + crypto_sign_ed25519_BYTES, data, size - crypto_sign_ed25519_BYTES);
free(data);
unsigned char *m = (unsigned char *)malloc(size);
unsigned long long mlen;
if (crypto_sign_ed25519_open(m, &mlen, signe, size, pk) == -1) {
printf("Failed checking sig.\n");
free(m);
free(signe);
goto fail;
}
free(m);
free(signe);
printf("Checked successfully.\n");
}
return 0;
fail:
printf("FAIL\n");
return 1;
}