]> source.dussan.org Git - rspamd.git/commitdiff
[Test] Add some specific unit tests for Rspamd x25519 DH
authorVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 31 Oct 2024 13:04:49 +0000 (13:04 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 31 Oct 2024 13:06:59 +0000 (13:06 +0000)
test/rspamd_cxx_unit_cryptobox.hxx

index 5829b1e4386db698a20f16041294ff0b1646f14a..18ccd98ce491490b1d697257ff67b190afc2bbe2 100644 (file)
@@ -21,6 +21,7 @@
 #include "libcryptobox/cryptobox.h"
 #include <string>
 #include <string_view>
+#include <array>
 
 TEST_SUITE("rspamd_cryptobox")
 {
@@ -177,6 +178,57 @@ TEST_SUITE("rspamd_cryptobox")
                g_free(out);
                g_free(decrypted);
        }
+
+       TEST_CASE("rspamd x25519 scalarmult")
+       {
+               rspamd_sk_t sk;
+
+               // Use a fixed zero secret key
+               memset(sk, 0, sizeof(sk));
+
+               // Use a well known public key
+               const char *pk = "k4nz984k36xmcynm1hr9kdbn6jhcxf4ggbrb1quay7f88rpm9kay";
+               gsize outlen;
+               auto *pk_decoded = rspamd_decode_base32(pk, strlen(pk), &outlen, RSPAMD_BASE32_DEFAULT);
+               const unsigned char expected[32] = {95, 76, 225, 188, 0, 26, 146, 94, 70, 249,
+                                                                                       90, 189, 35, 51, 1, 42, 9, 37, 94, 254, 204, 55, 198, 91, 180, 90,
+                                                                                       46, 217, 140, 226, 211, 90};
+               const auto expected_arr = std::to_array(expected);
+
+               CHECK(outlen == 32);
+               unsigned char out[32];
+               /* Clamp integer */
+               sk[0] &= 248;
+               sk[31] &= 127;
+               sk[31] |= 64;
+               CHECK(crypto_scalarmult(out, sk, pk_decoded) != -1);
+               auto out_arr = std::to_array(out);
+               CHECK(out_arr == expected_arr);
+       }
+
+       TEST_CASE("rspamd x25519 ecdh")
+       {
+               rspamd_sk_t sk;
+
+               // Use a fixed zero secret key
+               memset(sk, 0, sizeof(sk));
+
+               // Use a well known public key
+               const char *pk = "k4nz984k36xmcynm1hr9kdbn6jhcxf4ggbrb1quay7f88rpm9kay";
+               gsize outlen;
+               auto *pk_decoded = rspamd_decode_base32(pk, strlen(pk), &outlen, RSPAMD_BASE32_DEFAULT);
+               const unsigned char expected[32] = {61, 109, 220, 195, 100, 174, 127, 237, 148,
+                                                                                       122, 154, 61, 165, 83, 93, 105, 127, 166, 153, 112, 103, 224, 2, 200,
+                                                                                       136, 243, 73, 51, 8, 163, 150, 7};
+               const auto expected_arr = std::to_array(expected);
+
+               CHECK(outlen == 32);
+               unsigned char out[32];
+
+               rspamd_cryptobox_nm(out, pk_decoded, sk);
+               auto out_arr = std::to_array(out);
+               CHECK(out_arr == expected_arr);
+       }
 }
 
 #endif