Green Sky
b2ae9530a4
f1df709b87 feat: add ngc events 1b6c907235 refactor: Make event dispatch ordered by receive time. b7f9367f6f test: Upgrade cppcheck, fix some warnings. 766e62bc89 chore: Use `pkg_search_module` directly in cmake. 00ff078f91 cleanup: Use target_link_libraries directly in cmake. c58928cc89 chore: Add `IMPORTED_TARGET` to pkg-config packages. 895a6af122 cleanup: Remove NaCl support. 41dfb1c1c0 fix: unpack enum function names in event impl generator 447666d1a1 chore: Disable targets for cross-compilation. 572924e924 chore: Build a docker image with coverage info in it. 415cb78f5e cleanup: Some portability/warning fixes for Windows builds. 425216d9ec fix: Correct a use-after-free and fix some memory leaks. 4b1cfa3e08 refactor: Change all enum-like `#define` sequences into enums. d3c2704fa9 chore: Fix make_single_file to support core-only. 0ce46b644e refactor: Change the `TCP_PACKET_*` defines into an enum. 22cd38ad50 adopt event impl generation tool to #2392 f31ea1088a add the event impl generation tool 4e603bb613 refactor: Use `enum-from-int` rule from tokstyle. 19d8f180d6 chore: Update github actions `uses`. 6a895be0c7 test: Make esp32 build actually try to instantiate tox. 65d09c9bfb cleanup: Remove test net support. REVERT: e29e185c03 feat: add ngc events git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: f1df709b8792da4c0e946d826b11df77d565064d
148 lines
3.6 KiB
C
148 lines
3.6 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 "../../testing/misc_tools.h" // hex_string_to_bin
|
|
#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 != 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 (int i = 0; i < crypto_sign_ed25519_PUBLICKEYBYTES; ++i) {
|
|
printf("%02X", pk[i]);
|
|
}
|
|
|
|
printf("\nSecret key:\n");
|
|
|
|
for (int i = 0; i < crypto_sign_ed25519_SECRETKEYBYTES; ++i) {
|
|
printf("%02X", sk[i]);
|
|
}
|
|
|
|
printf("\n");
|
|
}
|
|
|
|
if (argc == 5 && argv[1][0] == 's') {
|
|
unsigned char *secret_key = hex_string_to_bin(argv[2]);
|
|
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, secret_key);
|
|
free(data);
|
|
free(secret_key);
|
|
|
|
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') {
|
|
unsigned char *public_key = hex_string_to_bin(argv[2]);
|
|
unsigned char *data;
|
|
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, public_key) == -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;
|
|
}
|