This commit is contained in:
Heiko Reese
2021-08-07 02:40:14 +02:00
parent 913d9720d6
commit 77c5cd392d
3 changed files with 97 additions and 4 deletions

49
src/genkey.c Normal file
View File

@ -0,0 +1,49 @@
#include <sodium.h>
void dumpkey(FILE* f, unsigned char * name, unsigned char * key, unsigned int keylen) {
fprintf(f, "const unsigned char %s[] = { ", name);
for(int i=0; i < keylen; i++) {
fprintf(f, "0x%02X", key[i]);
if (i < keylen-1) {
fprintf(f, ", ");
}
}
fprintf(f, " };\n");
}
int main(void)
{
if (sodium_init() < 0) {
fputs("Unable to initialize libsodium", stderr);
exit(128);
}
unsigned char key[crypto_secretbox_KEYBYTES];
crypto_secretbox_keygen(key);
FILE *keyfile = fopen("secretkey.h", "w+");
if (keyfile == NULL) {
fputs("Unable to open secretkey.h", stderr);
exit(129);
}
fputs("#ifndef EXIM4ENCRYPTSECRETKEY_H\n#define EXIM4ENCRYPTSECRETKEY_H\n\n", keyfile);
dumpkey(keyfile, "key", key, crypto_secretbox_KEYBYTES);
fprintf(keyfile, "unsigned int keylen = %u;\n", crypto_secretbox_KEYBYTES);
fputs("#endif // EXIM4ENCRYPTSECRETKEY_H\n", keyfile);
fclose(keyfile);
FILE *keyfilebin = fopen("secretkey.bin", "w+");
if (keyfilebin == NULL) {
fputs("Unable to open secretkey.bin", stderr);
exit(129);
}
fwrite(key, sizeof(key[0]), crypto_secretbox_KEYBYTES, keyfilebin);
fclose(keyfilebin);
exit(EXIT_SUCCESS);
}