tomato/other/fun/strkey.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

152 lines
4.1 KiB
C

/* strkey -- String in Public Key
*
* Generates Tox's key pairs, checking if a certain string is in the public key.
*
* Requires sodium library.
*
* There seem to be some problems with the code working on Windows -- it works
* when built in debug mode with MinGW 4.8, but it doesn't work correctly when
* built in release.
*
* Usage: strkey <offset> <string>
*
* Offset - an integer specifying exact byte offset position of the string you
* are looking for within a public key. When offset is negative, the program
* just looks for the desired string being somewhere, doesn't matter where, in
* the public key.
*
* String - a hex string that you want to have in your public key. It must have
* an even number of letters, since every two hexes map to a single byte of
* the public key.
*
* Examples:
* strkey 0 0123
* Looks for a public key that begins with "0123".
*
* strkey 1 0123
* Looks for a public key that has "0123" starting at its second byte, i.e. "XX0123...".
*
* strkey 2 0123
* Looks for a public key that has "0123" starting at its third byte, i.e. "XXXX0123...".
* (each two hexes represent a single byte of a public key)
*
* strkey -1 AF57CC
* Looks for a public key that contains "AF57CC", regardless of its position.
*
* To compile with gcc and sodium: gcc strkey.c -o strkey -lsodium
*/
#include <stdio.h>
#include <string.h>
#include <sodium.h>
#include "../../toxcore/ccompat.h"
#define PRINT_TRIES_COUNT
static void print_key(const unsigned char *key)
{
for (size_t i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) {
if (key[i] < 16) {
fprintf(stdout, "0");
}
fprintf(stdout, "%hhX", key[i]);
}
}
int main(int argc, char *argv[])
{
unsigned char public_key[crypto_box_PUBLICKEYBYTES]; // null terminator
unsigned char secret_key[crypto_box_SECRETKEYBYTES];
long int offset = 0;
size_t len;
unsigned char desired_bin[crypto_box_PUBLICKEYBYTES]; // null terminator
if (argc == 3) {
offset = strtol(argv[1], nullptr, 10);
if (offset <= 0) {
fprintf(stderr, "strtol() failed to convert \"%s\" into an integer\n", argv[1]);
exit(1);
}
char *desired_hex = argv[2];
len = strlen(desired_hex);
if (len % 2 != 0) {
fprintf(stderr, "Desired key should have an even number of letters\n");
exit(1);
}
size_t block_length = (offset < 0 ? 0 : offset) + len / 2;
if (block_length > crypto_box_PUBLICKEYBYTES) {
fprintf(stderr, "The given key with the given offset exceed public key's length\n");
exit(1);
}
// convert hex to bin
char *pos = desired_hex;
size_t i;
for (i = 0; i < len; pos += 2) {
unsigned int value;
sscanf(pos, "%02x", &value);
desired_bin[i] = value;
++i;
}
} else {
fprintf(stdout, "Usage: executable <byte offset> <desired hex string with even number of letters>\n");
exit(1);
}
len /= 2;
#ifdef PRINT_TRIES_COUNT
long long unsigned int tries = 0;
#endif
if (offset < 0) {
int found = 0;
do {
#ifdef PRINT_TRIES_COUNT
++tries;
#endif
crypto_box_keypair(public_key, secret_key);
for (uint32_t i = 0; i <= crypto_box_PUBLICKEYBYTES - len; ++i) {
if (memcmp(public_key + i, desired_bin, len) == 0) {
found = 1;
break;
}
}
} while (!found);
} else {
const unsigned char *p = public_key + offset;
do {
#ifdef PRINT_TRIES_COUNT
++tries;
#endif
crypto_box_keypair(public_key, secret_key);
} while (memcmp(p, desired_bin, len) != 0);
}
fprintf(stdout, "Public key: ");
print_key(public_key);
fprintf(stdout, "\n");
fprintf(stdout, "Private key: ");
print_key(secret_key);
fprintf(stdout, "\n");
#ifdef PRINT_TRIES_COUNT
fprintf(stdout, "Found the key pair on %llu try.\n", tries);
#endif
return 0;
}