fix: Standardized indention using "indent -linux"

This commit is contained in:
Heiko Reese
2021-08-11 01:52:11 +02:00
parent 675b37002e
commit 72549d0649
2 changed files with 35 additions and 25 deletions

View File

@ -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,20 +15,23 @@ 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];
sprintf(header_filename, "%s.h", filebase);
sprintf(raw_filename, "%s.raw", filebase);
sprintf(header_filename, "%s.h", filebase);
sprintf(raw_filename, "%s.raw", filebase);
// 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);
}

View File

@ -12,39 +12,43 @@
/*
* 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,
ciphertext, cipherlen,
sodium_base64_VARIANT_ORIGINAL);
sodium_bin2base64((char *const)outstring, outputsize,
ciphertext, cipherlen,
sodium_base64_VARIANT_ORIGINAL);
free(ciphertext);
// return base64-encoded ciphertext