mirror of
https://gitlab.kit.edu/kit/scc/sys/mail/exim-encrypt-dlfunc.git
synced 2025-12-06 08:33:56 +01:00
fix: Standardized indention using "indent -linux"
This commit is contained in:
26
src/genkey.c
26
src/genkey.c
@ -1,10 +1,13 @@
|
||||
#include <sodium.h>
|
||||
|
||||
void dump_key_as_c_code(FILE* f, const char * name, unsigned char * key, unsigned int keylen) {
|
||||
void
|
||||
dump_key_as_c_code(FILE * f, const char *name, unsigned char *key,
|
||||
unsigned int keylen)
|
||||
{
|
||||
fprintf(f, "const unsigned char %s[] = { ", name);
|
||||
for(int i=0; i < keylen; i++) {
|
||||
for (int i = 0; i < keylen; i++) {
|
||||
fprintf(f, "0x%02x", key[i]);
|
||||
if (i < keylen-1) {
|
||||
if (i < keylen - 1) {
|
||||
fprintf(f, ", ");
|
||||
}
|
||||
}
|
||||
@ -12,7 +15,10 @@ void dump_key_as_c_code(FILE* f, const char * name, unsigned char * key, unsigne
|
||||
fprintf(f, "const unsigned int %s_length = %d;\n", name, keylen);
|
||||
}
|
||||
|
||||
void write_key_files(const char * filebase, const char * varname, unsigned char * key, unsigned int keylen) {
|
||||
void
|
||||
write_key_files(const char *filebase, const char *varname,
|
||||
unsigned char *key, unsigned int keylen)
|
||||
{
|
||||
char header_filename[4096];
|
||||
char raw_filename[4096];
|
||||
|
||||
@ -22,10 +28,10 @@ void write_key_files(const char * filebase, const char * varname, unsigned char
|
||||
// open header file
|
||||
FILE *hfile = fopen(header_filename, "w+");
|
||||
if (hfile == NULL) {
|
||||
fprintf(stderr, "Unable to open %s for writing", header_filename);
|
||||
fprintf(stderr, "Unable to open %s for writing",
|
||||
header_filename);
|
||||
exit(129);
|
||||
}
|
||||
|
||||
// write key as C code
|
||||
dump_key_as_c_code(hfile, varname, key, keylen);
|
||||
|
||||
@ -38,7 +44,6 @@ void write_key_files(const char * filebase, const char * varname, unsigned char
|
||||
fprintf(stderr, "Unable to open %s for writing", raw_filename);
|
||||
exit(129);
|
||||
}
|
||||
|
||||
// write key
|
||||
fwrite(key, sizeof(key[0]), keylen, rfile);
|
||||
|
||||
@ -58,9 +63,10 @@ int main(void)
|
||||
unsigned char recipient_sk[crypto_box_SECRETKEYBYTES];
|
||||
crypto_box_keypair(recipient_pk, recipient_sk);
|
||||
|
||||
write_key_files("recipient_pk", "recipient_pk", recipient_pk, crypto_box_PUBLICKEYBYTES);
|
||||
write_key_files("recipient_sk", "recipient_sk", recipient_sk, crypto_box_SECRETKEYBYTES);
|
||||
write_key_files("recipient_pk", "recipient_pk", recipient_pk,
|
||||
crypto_box_PUBLICKEYBYTES);
|
||||
write_key_files("recipient_sk", "recipient_sk", recipient_sk,
|
||||
crypto_box_SECRETKEYBYTES);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
@ -12,37 +12,41 @@
|
||||
/*
|
||||
* Encrypt first argument with fixed public key from recipient_pk.h
|
||||
*/
|
||||
int sodium_crypto_box_seal_kit(uschar **yield, int argc, uschar *argv[]) {
|
||||
int sodium_crypto_box_seal_kit(uschar ** yield, int argc, uschar * argv[])
|
||||
{
|
||||
size_t messagelen;
|
||||
unsigned char * message;
|
||||
unsigned char *message;
|
||||
|
||||
if (sodium_init() == -1 ) {
|
||||
*yield = string_copy(US"Unable to initialize libsodium");
|
||||
if (sodium_init() == -1) {
|
||||
*yield = string_copy(US "Unable to initialize libsodium");
|
||||
return ERROR;
|
||||
}
|
||||
if (argc != 1) {
|
||||
*yield = string_sprintf("Wrong number of arguments (got %i, expected 1)", argc);
|
||||
*yield =
|
||||
string_sprintf
|
||||
("Wrong number of arguments (got %i, expected 1)", argc);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// get cleartext message
|
||||
message = argv[0];
|
||||
messagelen = strlen((const char *) message);
|
||||
messagelen = strlen((const char *)message);
|
||||
|
||||
// prepare buffer for ciphertext
|
||||
unsigned int cipherlen = messagelen + crypto_box_SEALBYTES;
|
||||
unsigned char * ciphertext = malloc(cipherlen);
|
||||
unsigned char *ciphertext = malloc(cipherlen);
|
||||
sodium_memzero(ciphertext, cipherlen);
|
||||
|
||||
// encrypt message
|
||||
crypto_box_seal(ciphertext, message, messagelen, recipient_pk);
|
||||
|
||||
// base64-encode the ciphertext
|
||||
unsigned int outputsize = sodium_base64_ENCODED_LEN(cipherlen, sodium_base64_VARIANT_ORIGINAL);
|
||||
unsigned char * outstring = malloc(outputsize);
|
||||
unsigned int outputsize =
|
||||
sodium_base64_ENCODED_LEN(cipherlen,
|
||||
sodium_base64_VARIANT_ORIGINAL);
|
||||
unsigned char *outstring = malloc(outputsize);
|
||||
sodium_memzero(outstring, outputsize);
|
||||
|
||||
sodium_bin2base64((char * const) outstring, outputsize,
|
||||
sodium_bin2base64((char *const)outstring, outputsize,
|
||||
ciphertext, cipherlen,
|
||||
sodium_base64_VARIANT_ORIGINAL);
|
||||
free(ciphertext);
|
||||
|
||||
Reference in New Issue
Block a user