|
|
@@ -19,6 +19,7 @@ |
|
|
|
#include "libcryptobox/keypair_private.h" |
|
|
|
#include "libutil/str_util.h" |
|
|
|
#include "libutil/printf.h" |
|
|
|
#include "contrib/libottery/ottery.h" |
|
|
|
|
|
|
|
const guchar encrypted_magic[7] = {'r', 'u', 'c', 'l', 'e', 'v', '1'}; |
|
|
|
|
|
|
@@ -977,3 +978,52 @@ rspamd_keypair_decrypt (struct rspamd_cryptobox_keypair *kp, |
|
|
|
|
|
|
|
return TRUE; |
|
|
|
} |
|
|
|
gboolean |
|
|
|
rspamd_keypair_encrypt (struct rspamd_cryptobox_keypair *kp, |
|
|
|
const guchar *in, gsize inlen, |
|
|
|
guchar **out, gsize *outlen, |
|
|
|
GError **err) |
|
|
|
{ |
|
|
|
guchar *nonce, *mac, *data, *pubkey; |
|
|
|
struct rspamd_cryptobox_keypair *local; |
|
|
|
gsize olen; |
|
|
|
|
|
|
|
g_assert (kp != NULL); |
|
|
|
g_assert (in != NULL); |
|
|
|
|
|
|
|
if (kp->type != RSPAMD_KEYPAIR_KEX) { |
|
|
|
g_set_error (err, rspamd_keypair_quark (), EINVAL, |
|
|
|
"invalid keypair type"); |
|
|
|
|
|
|
|
return FALSE; |
|
|
|
} |
|
|
|
|
|
|
|
local = rspamd_keypair_new (kp->type, kp->alg); |
|
|
|
|
|
|
|
olen = inlen + sizeof (encrypted_magic) + |
|
|
|
rspamd_cryptobox_pk_bytes (kp->alg) + |
|
|
|
rspamd_cryptobox_mac_bytes (kp->alg) + |
|
|
|
rspamd_cryptobox_nonce_bytes (kp->alg); |
|
|
|
*out = g_malloc (olen); |
|
|
|
memcpy (*out, encrypted_magic, sizeof (encrypted_magic)); |
|
|
|
pubkey = *out + sizeof (encrypted_magic); |
|
|
|
mac = pubkey + rspamd_cryptobox_pk_bytes (kp->alg); |
|
|
|
nonce = mac + rspamd_cryptobox_mac_bytes (kp->alg); |
|
|
|
data = nonce + rspamd_cryptobox_nonce_bytes (kp->alg); |
|
|
|
|
|
|
|
ottery_rand_bytes (nonce, rspamd_cryptobox_nonce_bytes (kp->alg)); |
|
|
|
memcpy (data, in, inlen); |
|
|
|
memcpy (pubkey, rspamd_keypair_component (kp, |
|
|
|
RSPAMD_KEYPAIR_COMPONENT_PK, NULL), |
|
|
|
rspamd_cryptobox_pk_bytes (kp->alg)); |
|
|
|
rspamd_cryptobox_encrypt_inplace (data, inlen, nonce, pubkey, |
|
|
|
rspamd_keypair_component (local, RSPAMD_KEYPAIR_COMPONENT_SK, NULL), |
|
|
|
mac, kp->alg); |
|
|
|
rspamd_keypair_unref (local); |
|
|
|
|
|
|
|
if (outlen) { |
|
|
|
*outlen = olen; |
|
|
|
} |
|
|
|
|
|
|
|
return TRUE; |
|
|
|
} |