#include #include #include #include /* Local encryption key */ #include "recipient_pk.h" /* Exim4 dlfunc API header */ #include int kitencrypt(uschar **yield, int argc, uschar *argv[]) { int sinit; size_t messagelen; unsigned char * message; sinit = sodium_init(); if (sinit == -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); return ERROR; } // get cleartext message message = argv[0]; messagelen = strlen(message); // prepare buffer for ciphertext unsigned int cipherlen = messagelen + crypto_box_SEALBYTES; unsigned char * ciphertext = malloc(cipherlen); explicit_bzero(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_URLSAFE); unsigned char * outstring = malloc(outputsize); explicit_bzero(outstring, outputsize); sodium_bin2base64(outstring, outputsize, ciphertext, cipherlen, sodium_base64_VARIANT_URLSAFE); free(ciphertext); *yield = string_copy(outstring); free(outstring); return OK; }