summaryrefslogtreecommitdiffstats
path: root/src/libcryptobox/keypair.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libcryptobox/keypair.c')
-rw-r--r--src/libcryptobox/keypair.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/libcryptobox/keypair.c b/src/libcryptobox/keypair.c
index 21b497130..c8fa5633a 100644
--- a/src/libcryptobox/keypair.c
+++ b/src/libcryptobox/keypair.c
@@ -978,6 +978,7 @@ rspamd_keypair_decrypt (struct rspamd_cryptobox_keypair *kp,
return TRUE;
}
+
gboolean
rspamd_keypair_encrypt (struct rspamd_cryptobox_keypair *kp,
const guchar *in, gsize inlen,
@@ -1026,4 +1027,53 @@ rspamd_keypair_encrypt (struct rspamd_cryptobox_keypair *kp,
}
return TRUE;
+}
+
+gboolean
+rspamd_pubkey_encrypt (struct rspamd_cryptobox_pubkey *pk,
+ 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 (pk != NULL);
+ g_assert (in != NULL);
+
+ if (pk->type != RSPAMD_KEYPAIR_KEX) {
+ g_set_error (err, rspamd_keypair_quark (), EINVAL,
+ "invalid pubkey type");
+
+ return FALSE;
+ }
+
+ local = rspamd_keypair_new (pk->type, pk->alg);
+
+ olen = inlen + sizeof (encrypted_magic) +
+ rspamd_cryptobox_pk_bytes (pk->alg) +
+ rspamd_cryptobox_mac_bytes (pk->alg) +
+ rspamd_cryptobox_nonce_bytes (pk->alg);
+ *out = g_malloc (olen);
+ memcpy (*out, encrypted_magic, sizeof (encrypted_magic));
+ pubkey = *out + sizeof (encrypted_magic);
+ mac = pubkey + rspamd_cryptobox_pk_bytes (pk->alg);
+ nonce = mac + rspamd_cryptobox_mac_bytes (pk->alg);
+ data = nonce + rspamd_cryptobox_nonce_bytes (pk->alg);
+
+ ottery_rand_bytes (nonce, rspamd_cryptobox_nonce_bytes (pk->alg));
+ memcpy (data, in, inlen);
+ memcpy (pubkey, rspamd_pubkey_get_pk (pk, NULL),
+ rspamd_cryptobox_pk_bytes (pk->alg));
+ rspamd_cryptobox_encrypt_inplace (data, inlen, nonce, pubkey,
+ rspamd_keypair_component (local, RSPAMD_KEYPAIR_COMPONENT_SK, NULL),
+ mac, pk->alg);
+ rspamd_keypair_unref (local);
+
+ if (outlen) {
+ *outlen = olen;
+ }
+
+ return TRUE;
} \ No newline at end of file