You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cryptobox.c 40KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* Workaround for memset_s */
  17. #ifdef __APPLE__
  18. #define __STDC_WANT_LIB_EXT1__ 1
  19. #include <string.h>
  20. #endif
  21. #include "config.h"
  22. #include "cryptobox.h"
  23. #include "platform_config.h"
  24. #include "chacha20/chacha.h"
  25. #include "catena/catena.h"
  26. #include "base64/base64.h"
  27. #include "ottery.h"
  28. #include "printf.h"
  29. #define XXH_INLINE_ALL
  30. #define XXH_PRIVATE_API
  31. #include "xxhash.h"
  32. #define MUM_TARGET_INDEPENDENT_HASH 1 /* For 32/64 bit equal hashes */
  33. #include "../../contrib/mumhash/mum.h"
  34. #include "../../contrib/t1ha/t1ha.h"
  35. #ifdef HAVE_CPUID_H
  36. #include <cpuid.h>
  37. #endif
  38. #ifdef HAVE_OPENSSL
  39. #include <openssl/opensslv.h>
  40. /* Openssl >= 1.0.1d is required for GCM verification */
  41. #if OPENSSL_VERSION_NUMBER >= 0x1000104fL
  42. #define HAVE_USABLE_OPENSSL 1
  43. #endif
  44. #endif
  45. #ifdef HAVE_USABLE_OPENSSL
  46. #include <openssl/evp.h>
  47. #include <openssl/ec.h>
  48. #include <openssl/ecdh.h>
  49. #include <openssl/ecdsa.h>
  50. #include <openssl/rand.h>
  51. #define CRYPTOBOX_CURVE_NID NID_X9_62_prime256v1
  52. #endif
  53. #include <signal.h>
  54. #include <setjmp.h>
  55. #include <stdalign.h>
  56. #include <sodium.h>
  57. unsigned cpu_config = 0;
  58. static gboolean cryptobox_loaded = FALSE;
  59. static const guchar n0[16] = {0};
  60. #define CRYPTOBOX_ALIGNMENT 16
  61. #define cryptobox_align_ptr(p, a) \
  62. (void *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))
  63. static void
  64. rspamd_cryptobox_cpuid (gint cpu[4], gint info)
  65. {
  66. guint32 __attribute__ ((unused)) eax, __attribute__ ((unused)) ecx = 0, __attribute__ ((unused)) ebx = 0, __attribute__ ((unused)) edx = 0;
  67. eax = info;
  68. #if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
  69. # if defined( __i386__ ) && defined ( __PIC__ )
  70. /* in case of PIC under 32-bit EBX cannot be clobbered */
  71. __asm__ volatile ("movl %%ebx, %%edi \n\t cpuid \n\t xchgl %%ebx, %%edi" : "=D" (ebx),
  72. "+a" (eax), "+c" (ecx), "=d" (edx));
  73. # else
  74. __asm__ volatile ("cpuid" : "+b" (ebx), "+a" (eax), "+c" (ecx), "=d" (edx));
  75. # endif
  76. cpu[0] = eax; cpu[1] = ebx; cpu[2] = ecx; cpu[3] = edx;
  77. #else
  78. memset (cpu, 0, sizeof (gint) * 4);
  79. #endif
  80. }
  81. static sig_atomic_t ok = 0;
  82. static jmp_buf j;
  83. __attribute__((noreturn))
  84. static void
  85. rspamd_cryptobox_ill_handler (int signo)
  86. {
  87. ok = 0;
  88. longjmp (j, -1);
  89. }
  90. static gboolean
  91. rspamd_cryptobox_test_instr (gint instr)
  92. {
  93. void (*old_handler) (int);
  94. guint32 rd;
  95. #if defined(__GNUC__)
  96. ok = 1;
  97. old_handler = signal (SIGILL, rspamd_cryptobox_ill_handler);
  98. if (setjmp (j) != 0) {
  99. signal (SIGILL, old_handler);
  100. return FALSE;
  101. }
  102. switch (instr) {
  103. #if defined HAVE_SSE2 && defined (__x86_64__)
  104. case CPUID_SSE2:
  105. __asm__ volatile ("psubb %xmm0, %xmm0");
  106. break;
  107. case CPUID_RDRAND:
  108. /* Use byte code here for compatibility */
  109. __asm__ volatile (".byte 0x0f,0xc7,0xf0; setc %1"
  110. : "=a" (rd), "=qm" (ok)
  111. :
  112. : "edx"
  113. );
  114. break;
  115. #endif
  116. #ifdef HAVE_SSE3
  117. case CPUID_SSE3:
  118. __asm__ volatile ("movshdup %xmm0, %xmm0");
  119. break;
  120. #endif
  121. #ifdef HAVE_SSSE3
  122. case CPUID_SSSE3:
  123. __asm__ volatile ("pshufb %xmm0, %xmm0");
  124. break;
  125. #endif
  126. #ifdef HAVE_SSE41
  127. case CPUID_SSE41:
  128. __asm__ volatile ("pcmpeqq %xmm0, %xmm0");
  129. break;
  130. #endif
  131. #if defined HAVE_SSE42 && defined(__x86_64__)
  132. case CPUID_SSE42:
  133. __asm__ volatile ("pushq %rax\n"
  134. "xorq %rax, %rax\n"
  135. "crc32 %rax, %rax\n"
  136. "popq %rax");
  137. break;
  138. #endif
  139. #ifdef HAVE_AVX
  140. case CPUID_AVX:
  141. __asm__ volatile ("vpaddq %xmm0, %xmm0, %xmm0");
  142. break;
  143. #endif
  144. #ifdef HAVE_AVX2
  145. case CPUID_AVX2:
  146. __asm__ volatile ("vpaddq %ymm0, %ymm0, %ymm0");\
  147. break;
  148. #endif
  149. default:
  150. return FALSE;
  151. break;
  152. }
  153. signal (SIGILL, old_handler);
  154. #endif
  155. (void)rd; /* Silence warning */
  156. /* We actually never return here if SIGILL has been caught */
  157. return ok == 1;
  158. }
  159. struct rspamd_cryptobox_library_ctx*
  160. rspamd_cryptobox_init (void)
  161. {
  162. gint cpu[4], nid;
  163. const guint32 osxsave_mask = (1 << 27);
  164. const guint32 fma_movbe_osxsave_mask = ((1 << 12) | (1 << 22) | (1 << 27));
  165. const guint32 avx2_bmi12_mask = (1 << 5) | (1 << 3) | (1 << 8);
  166. gulong bit;
  167. static struct rspamd_cryptobox_library_ctx *ctx;
  168. GString *buf;
  169. if (cryptobox_loaded) {
  170. /* Ignore reload attempts */
  171. return ctx;
  172. }
  173. cryptobox_loaded = TRUE;
  174. ctx = g_malloc0 (sizeof (*ctx));
  175. rspamd_cryptobox_cpuid (cpu, 0);
  176. nid = cpu[0];
  177. rspamd_cryptobox_cpuid (cpu, 1);
  178. if (nid > 1) {
  179. if ((cpu[3] & ((guint32)1 << 26))) {
  180. if (rspamd_cryptobox_test_instr (CPUID_SSE2)) {
  181. cpu_config |= CPUID_SSE2;
  182. }
  183. }
  184. if ((cpu[2] & ((guint32)1 << 0))) {
  185. if (rspamd_cryptobox_test_instr (CPUID_SSE3)) {
  186. cpu_config |= CPUID_SSE3;
  187. }
  188. }
  189. if ((cpu[2] & ((guint32)1 << 9))) {
  190. if (rspamd_cryptobox_test_instr (CPUID_SSSE3)) {
  191. cpu_config |= CPUID_SSSE3;
  192. }
  193. }
  194. if ((cpu[2] & ((guint32)1 << 19))) {
  195. if (rspamd_cryptobox_test_instr (CPUID_SSE41)) {
  196. cpu_config |= CPUID_SSE41;
  197. }
  198. }
  199. if ((cpu[2] & ((guint32)1 << 20))) {
  200. if (rspamd_cryptobox_test_instr (CPUID_SSE42)) {
  201. cpu_config |= CPUID_SSE42;
  202. }
  203. }
  204. if ((cpu[2] & ((guint32)1 << 30))) {
  205. if (rspamd_cryptobox_test_instr (CPUID_RDRAND)) {
  206. cpu_config |= CPUID_RDRAND;
  207. }
  208. }
  209. /* OSXSAVE */
  210. if ((cpu[2] & osxsave_mask) == osxsave_mask) {
  211. if ((cpu[2] & ((guint32)1 << 28))) {
  212. if (rspamd_cryptobox_test_instr (CPUID_AVX)) {
  213. cpu_config |= CPUID_AVX;
  214. }
  215. }
  216. if (nid >= 7 &&
  217. (cpu[2] & fma_movbe_osxsave_mask) == fma_movbe_osxsave_mask) {
  218. rspamd_cryptobox_cpuid (cpu, 7);
  219. if ((cpu[1] & avx2_bmi12_mask) == avx2_bmi12_mask) {
  220. if (rspamd_cryptobox_test_instr (CPUID_AVX2)) {
  221. cpu_config |= CPUID_AVX2;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. buf = g_string_new ("");
  228. for (bit = 0x1; bit != 0; bit <<= 1) {
  229. if (cpu_config & bit) {
  230. switch (bit) {
  231. case CPUID_SSE2:
  232. rspamd_printf_gstring (buf, "sse2, ");
  233. break;
  234. case CPUID_SSE3:
  235. rspamd_printf_gstring (buf, "sse3, ");
  236. break;
  237. case CPUID_SSSE3:
  238. rspamd_printf_gstring (buf, "ssse3, ");
  239. break;
  240. case CPUID_SSE41:
  241. rspamd_printf_gstring (buf, "sse4.1, ");
  242. break;
  243. case CPUID_SSE42:
  244. rspamd_printf_gstring (buf, "sse4.2, ");
  245. break;
  246. case CPUID_AVX:
  247. rspamd_printf_gstring (buf, "avx, ");
  248. break;
  249. case CPUID_AVX2:
  250. rspamd_printf_gstring (buf, "avx2, ");
  251. break;
  252. case CPUID_RDRAND:
  253. rspamd_printf_gstring (buf, "rdrand, ");
  254. break;
  255. default:
  256. break; /* Silence warning */
  257. }
  258. }
  259. }
  260. if (buf->len > 2) {
  261. /* Trim last chars */
  262. g_string_erase (buf, buf->len - 2, 2);
  263. }
  264. ctx->cpu_extensions = buf->str;
  265. g_string_free (buf, FALSE);
  266. ctx->cpu_config = cpu_config;
  267. g_assert (sodium_init () != -1);
  268. ctx->chacha20_impl = chacha_load ();
  269. ctx->base64_impl = base64_load ();
  270. #if defined(HAVE_USABLE_OPENSSL) && (OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER))
  271. /* Needed for old openssl api, not sure about LibreSSL */
  272. ERR_load_EC_strings ();
  273. ERR_load_RAND_strings ();
  274. ERR_load_EVP_strings ();
  275. #endif
  276. return ctx;
  277. }
  278. void
  279. rspamd_cryptobox_deinit (struct rspamd_cryptobox_library_ctx *ctx)
  280. {
  281. if (ctx) {
  282. g_free (ctx->cpu_extensions);
  283. g_free (ctx);
  284. }
  285. }
  286. void
  287. rspamd_cryptobox_keypair (rspamd_pk_t pk, rspamd_sk_t sk,
  288. enum rspamd_cryptobox_mode mode)
  289. {
  290. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  291. ottery_rand_bytes (sk, rspamd_cryptobox_MAX_SKBYTES);
  292. sk[0] &= 248;
  293. sk[31] &= 127;
  294. sk[31] |= 64;
  295. crypto_scalarmult_base (pk, sk);
  296. }
  297. else {
  298. #ifndef HAVE_USABLE_OPENSSL
  299. g_assert (0);
  300. #else
  301. EC_KEY *ec_sec;
  302. const BIGNUM *bn_sec;
  303. BIGNUM *bn_pub;
  304. const EC_POINT *ec_pub;
  305. gint len;
  306. ec_sec = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID);
  307. g_assert (ec_sec != NULL);
  308. g_assert (EC_KEY_generate_key (ec_sec) != 0);
  309. bn_sec = EC_KEY_get0_private_key (ec_sec);
  310. g_assert (bn_sec != NULL);
  311. ec_pub = EC_KEY_get0_public_key (ec_sec);
  312. g_assert (ec_pub != NULL);
  313. bn_pub = EC_POINT_point2bn (EC_KEY_get0_group (ec_sec),
  314. ec_pub, POINT_CONVERSION_UNCOMPRESSED, NULL, NULL);
  315. len = BN_num_bytes (bn_sec);
  316. g_assert (len <= (gint)sizeof (rspamd_sk_t));
  317. BN_bn2bin (bn_sec, sk);
  318. len = BN_num_bytes (bn_pub);
  319. g_assert (len <= (gint)rspamd_cryptobox_pk_bytes (mode));
  320. BN_bn2bin (bn_pub, pk);
  321. BN_free (bn_pub);
  322. EC_KEY_free (ec_sec);
  323. #endif
  324. }
  325. }
  326. void
  327. rspamd_cryptobox_keypair_sig (rspamd_sig_pk_t pk, rspamd_sig_sk_t sk,
  328. enum rspamd_cryptobox_mode mode)
  329. {
  330. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  331. crypto_sign_keypair (pk, sk);
  332. }
  333. else {
  334. #ifndef HAVE_USABLE_OPENSSL
  335. g_assert (0);
  336. #else
  337. EC_KEY *ec_sec;
  338. const BIGNUM *bn_sec;
  339. BIGNUM *bn_pub;
  340. const EC_POINT *ec_pub;
  341. gint len;
  342. ec_sec = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID);
  343. g_assert (ec_sec != NULL);
  344. g_assert (EC_KEY_generate_key (ec_sec) != 0);
  345. bn_sec = EC_KEY_get0_private_key (ec_sec);
  346. g_assert (bn_sec != NULL);
  347. ec_pub = EC_KEY_get0_public_key (ec_sec);
  348. g_assert (ec_pub != NULL);
  349. bn_pub = EC_POINT_point2bn (EC_KEY_get0_group (ec_sec),
  350. ec_pub, POINT_CONVERSION_UNCOMPRESSED, NULL, NULL);
  351. len = BN_num_bytes (bn_sec);
  352. g_assert (len <= (gint)sizeof (rspamd_sk_t));
  353. BN_bn2bin (bn_sec, sk);
  354. len = BN_num_bytes (bn_pub);
  355. g_assert (len <= (gint)rspamd_cryptobox_pk_bytes (mode));
  356. BN_bn2bin (bn_pub, pk);
  357. BN_free (bn_pub);
  358. EC_KEY_free (ec_sec);
  359. #endif
  360. }
  361. }
  362. void
  363. rspamd_cryptobox_nm (rspamd_nm_t nm,
  364. const rspamd_pk_t pk, const rspamd_sk_t sk,
  365. enum rspamd_cryptobox_mode mode)
  366. {
  367. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  368. guchar s[32];
  369. guchar e[32];
  370. memcpy (e, sk, 32);
  371. e[0] &= 248;
  372. e[31] &= 127;
  373. e[31] |= 64;
  374. if (crypto_scalarmult (s, e, pk) != -1) {
  375. hchacha (s, n0, nm, 20);
  376. }
  377. rspamd_explicit_memzero (e, 32);
  378. }
  379. else {
  380. #ifndef HAVE_USABLE_OPENSSL
  381. g_assert (0);
  382. #else
  383. EC_KEY *lk;
  384. EC_POINT *ec_pub;
  385. BIGNUM *bn_pub, *bn_sec;
  386. gint len;
  387. guchar s[32];
  388. lk = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID);
  389. g_assert (lk != NULL);
  390. bn_pub = BN_bin2bn (pk, rspamd_cryptobox_pk_bytes (mode), NULL);
  391. g_assert (bn_pub != NULL);
  392. bn_sec = BN_bin2bn (sk, sizeof (rspamd_sk_t), NULL);
  393. g_assert (bn_sec != NULL);
  394. g_assert (EC_KEY_set_private_key (lk, bn_sec) == 1);
  395. ec_pub = EC_POINT_bn2point (EC_KEY_get0_group (lk), bn_pub, NULL, NULL);
  396. g_assert (ec_pub != NULL);
  397. len = ECDH_compute_key (s, sizeof (s), ec_pub, lk, NULL);
  398. g_assert (len == sizeof (s));
  399. /* Still do hchacha iteration since we are not using SHA1 KDF */
  400. hchacha (s, n0, nm, 20);
  401. EC_KEY_free (lk);
  402. EC_POINT_free (ec_pub);
  403. BN_free (bn_sec);
  404. BN_free (bn_pub);
  405. #endif
  406. }
  407. }
  408. void
  409. rspamd_cryptobox_sign (guchar *sig, unsigned long long *siglen_p,
  410. const guchar *m, gsize mlen,
  411. const rspamd_sk_t sk,
  412. enum rspamd_cryptobox_mode mode)
  413. {
  414. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  415. crypto_sign_detached (sig, siglen_p, m, mlen, sk);
  416. }
  417. else {
  418. #ifndef HAVE_USABLE_OPENSSL
  419. g_assert (0);
  420. #else
  421. EC_KEY *lk;
  422. BIGNUM *bn_sec, *kinv = NULL, *rp = NULL;
  423. EVP_MD_CTX *sha_ctx;
  424. unsigned char h[64];
  425. guint diglen = rspamd_cryptobox_signature_bytes (mode);
  426. /* Prehash */
  427. sha_ctx = EVP_MD_CTX_create ();
  428. g_assert (EVP_DigestInit (sha_ctx, EVP_sha512()) == 1);
  429. EVP_DigestUpdate (sha_ctx, m, mlen);
  430. EVP_DigestFinal (sha_ctx, h, NULL);
  431. /* Key setup */
  432. lk = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID);
  433. g_assert (lk != NULL);
  434. bn_sec = BN_bin2bn (sk, sizeof (rspamd_sk_t), NULL);
  435. g_assert (bn_sec != NULL);
  436. g_assert (EC_KEY_set_private_key (lk, bn_sec) == 1);
  437. /* ECDSA */
  438. g_assert (ECDSA_sign_setup (lk, NULL, &kinv, &rp) == 1);
  439. g_assert (ECDSA_sign_ex (0, h, sizeof (h), sig,
  440. &diglen, kinv, rp, lk) == 1);
  441. g_assert (diglen <= sizeof (rspamd_signature_t));
  442. if (siglen_p) {
  443. *siglen_p = diglen;
  444. }
  445. EC_KEY_free (lk);
  446. EVP_MD_CTX_destroy (sha_ctx);
  447. BN_free (bn_sec);
  448. BN_free (kinv);
  449. BN_free (rp);
  450. #endif
  451. }
  452. }
  453. bool
  454. rspamd_cryptobox_verify (const guchar *sig,
  455. gsize siglen,
  456. const guchar *m,
  457. gsize mlen,
  458. const rspamd_pk_t pk,
  459. enum rspamd_cryptobox_mode mode)
  460. {
  461. bool ret = false;
  462. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  463. if (siglen == rspamd_cryptobox_signature_bytes (RSPAMD_CRYPTOBOX_MODE_25519)) {
  464. ret = (crypto_sign_verify_detached (sig, m, mlen, pk) == 0);
  465. }
  466. }
  467. else {
  468. #ifndef HAVE_USABLE_OPENSSL
  469. g_assert (0);
  470. #else
  471. EC_KEY *lk;
  472. EC_POINT *ec_pub;
  473. BIGNUM *bn_pub;
  474. EVP_MD_CTX *sha_ctx;
  475. unsigned char h[64];
  476. /* Prehash */
  477. sha_ctx = EVP_MD_CTX_create ();
  478. g_assert (EVP_DigestInit (sha_ctx, EVP_sha512()) == 1);
  479. EVP_DigestUpdate (sha_ctx, m, mlen);
  480. EVP_DigestFinal (sha_ctx, h, NULL);
  481. /* Key setup */
  482. lk = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID);
  483. g_assert (lk != NULL);
  484. bn_pub = BN_bin2bn (pk, rspamd_cryptobox_pk_bytes (mode), NULL);
  485. g_assert (bn_pub != NULL);
  486. ec_pub = EC_POINT_bn2point (EC_KEY_get0_group (lk), bn_pub, NULL, NULL);
  487. g_assert (ec_pub != NULL);
  488. g_assert (EC_KEY_set_public_key (lk, ec_pub) == 1);
  489. /* ECDSA */
  490. ret = ECDSA_verify (0, h, sizeof (h), sig, siglen, lk) == 1;
  491. EC_KEY_free (lk);
  492. EVP_MD_CTX_destroy (sha_ctx);
  493. BN_free (bn_pub);
  494. EC_POINT_free (ec_pub);
  495. #endif
  496. }
  497. return ret;
  498. }
  499. static gsize
  500. rspamd_cryptobox_encrypt_ctx_len (enum rspamd_cryptobox_mode mode)
  501. {
  502. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  503. return sizeof (chacha_state) + CRYPTOBOX_ALIGNMENT;
  504. }
  505. else {
  506. #ifndef HAVE_USABLE_OPENSSL
  507. g_assert (0);
  508. #else
  509. return sizeof (EVP_CIPHER_CTX *) + CRYPTOBOX_ALIGNMENT;
  510. #endif
  511. }
  512. return 0;
  513. }
  514. static gsize
  515. rspamd_cryptobox_auth_ctx_len (enum rspamd_cryptobox_mode mode)
  516. {
  517. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  518. return sizeof (crypto_onetimeauth_state) + RSPAMD_ALIGNOF(crypto_onetimeauth_state);
  519. }
  520. else {
  521. #ifndef HAVE_USABLE_OPENSSL
  522. g_assert (0);
  523. #else
  524. return sizeof (void *);
  525. #endif
  526. }
  527. return 0;
  528. }
  529. static void *
  530. rspamd_cryptobox_encrypt_init (void *enc_ctx, const rspamd_nonce_t nonce,
  531. const rspamd_nm_t nm,
  532. enum rspamd_cryptobox_mode mode)
  533. {
  534. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  535. chacha_state *s;
  536. s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT);
  537. xchacha_init (s,
  538. (const chacha_key *) nm,
  539. (const chacha_iv24 *) nonce,
  540. 20);
  541. return s;
  542. }
  543. else {
  544. #ifndef HAVE_USABLE_OPENSSL
  545. g_assert (0);
  546. #else
  547. EVP_CIPHER_CTX **s;
  548. s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT);
  549. memset (s, 0, sizeof (*s));
  550. *s = EVP_CIPHER_CTX_new ();
  551. g_assert (EVP_EncryptInit_ex (*s, EVP_aes_256_gcm (), NULL, NULL, NULL) == 1);
  552. g_assert (EVP_CIPHER_CTX_ctrl (*s, EVP_CTRL_GCM_SET_IVLEN,
  553. rspamd_cryptobox_nonce_bytes (mode), NULL) == 1);
  554. g_assert (EVP_EncryptInit_ex (*s, NULL, NULL, nm, nonce) == 1);
  555. return s;
  556. #endif
  557. }
  558. return NULL;
  559. }
  560. static void *
  561. rspamd_cryptobox_auth_init (void *auth_ctx, void *enc_ctx,
  562. enum rspamd_cryptobox_mode mode)
  563. {
  564. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  565. crypto_onetimeauth_state *mac_ctx;
  566. guchar RSPAMD_ALIGNED(32) subkey[CHACHA_BLOCKBYTES];
  567. mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT);
  568. memset (subkey, 0, sizeof (subkey));
  569. chacha_update (enc_ctx, subkey, subkey, sizeof (subkey));
  570. crypto_onetimeauth_init (mac_ctx, subkey);
  571. rspamd_explicit_memzero (subkey, sizeof (subkey));
  572. return mac_ctx;
  573. }
  574. else {
  575. #ifndef HAVE_USABLE_OPENSSL
  576. g_assert (0);
  577. #else
  578. auth_ctx = enc_ctx;
  579. return auth_ctx;
  580. #endif
  581. }
  582. return NULL;
  583. }
  584. static gboolean
  585. rspamd_cryptobox_encrypt_update (void *enc_ctx, const guchar *in, gsize inlen,
  586. guchar *out, gsize *outlen,
  587. enum rspamd_cryptobox_mode mode)
  588. {
  589. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  590. gsize r;
  591. chacha_state *s;
  592. s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT);
  593. r = chacha_update (s, in, out, inlen);
  594. if (outlen != NULL) {
  595. *outlen = r;
  596. }
  597. return TRUE;
  598. }
  599. else {
  600. #ifndef HAVE_USABLE_OPENSSL
  601. g_assert (0);
  602. #else
  603. EVP_CIPHER_CTX **s = enc_ctx;
  604. gint r;
  605. r = inlen;
  606. g_assert (EVP_EncryptUpdate (*s, out, &r, in, inlen) == 1);
  607. if (outlen) {
  608. *outlen = r;
  609. }
  610. return TRUE;
  611. #endif
  612. }
  613. return FALSE;
  614. }
  615. static gboolean
  616. rspamd_cryptobox_auth_update (void *auth_ctx, const guchar *in, gsize inlen,
  617. enum rspamd_cryptobox_mode mode)
  618. {
  619. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  620. crypto_onetimeauth_state *mac_ctx;
  621. mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT);
  622. crypto_onetimeauth_update (mac_ctx, in, inlen);
  623. return TRUE;
  624. }
  625. else {
  626. #ifndef HAVE_USABLE_OPENSSL
  627. g_assert (0);
  628. #else
  629. return TRUE;
  630. #endif
  631. }
  632. return FALSE;
  633. }
  634. static gsize
  635. rspamd_cryptobox_encrypt_final (void *enc_ctx, guchar *out, gsize remain,
  636. enum rspamd_cryptobox_mode mode)
  637. {
  638. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  639. chacha_state *s;
  640. s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT);
  641. return chacha_final (s, out);
  642. }
  643. else {
  644. #ifndef HAVE_USABLE_OPENSSL
  645. g_assert (0);
  646. #else
  647. EVP_CIPHER_CTX **s = enc_ctx;
  648. gint r = remain;
  649. g_assert (EVP_EncryptFinal_ex (*s, out, &r) == 1);
  650. return r;
  651. #endif
  652. }
  653. return 0;
  654. }
  655. static gboolean
  656. rspamd_cryptobox_auth_final (void *auth_ctx, rspamd_mac_t sig,
  657. enum rspamd_cryptobox_mode mode)
  658. {
  659. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  660. crypto_onetimeauth_state *mac_ctx;
  661. mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT);
  662. crypto_onetimeauth_final (mac_ctx, sig);
  663. return TRUE;
  664. }
  665. else {
  666. #ifndef HAVE_USABLE_OPENSSL
  667. g_assert (0);
  668. #else
  669. EVP_CIPHER_CTX **s = auth_ctx;
  670. g_assert (EVP_CIPHER_CTX_ctrl (*s, EVP_CTRL_GCM_GET_TAG,
  671. sizeof (rspamd_mac_t), sig) == 1);
  672. return TRUE;
  673. #endif
  674. }
  675. return FALSE;
  676. }
  677. static void *
  678. rspamd_cryptobox_decrypt_init (void *enc_ctx, const rspamd_nonce_t nonce,
  679. const rspamd_nm_t nm,
  680. enum rspamd_cryptobox_mode mode)
  681. {
  682. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  683. chacha_state *s;
  684. s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT);
  685. xchacha_init (s,
  686. (const chacha_key *) nm,
  687. (const chacha_iv24 *) nonce,
  688. 20);
  689. return s;
  690. }
  691. else {
  692. #ifndef HAVE_USABLE_OPENSSL
  693. g_assert (0);
  694. #else
  695. EVP_CIPHER_CTX **s;
  696. s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT);
  697. memset (s, 0, sizeof (*s));
  698. *s = EVP_CIPHER_CTX_new ();
  699. g_assert (EVP_DecryptInit_ex(*s, EVP_aes_256_gcm (), NULL, NULL, NULL) == 1);
  700. g_assert (EVP_CIPHER_CTX_ctrl (*s, EVP_CTRL_GCM_SET_IVLEN,
  701. rspamd_cryptobox_nonce_bytes (mode), NULL) == 1);
  702. g_assert (EVP_DecryptInit_ex (*s, NULL, NULL, nm, nonce) == 1);
  703. return s;
  704. #endif
  705. }
  706. return NULL;
  707. }
  708. static void *
  709. rspamd_cryptobox_auth_verify_init (void *auth_ctx, void *enc_ctx,
  710. enum rspamd_cryptobox_mode mode)
  711. {
  712. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  713. crypto_onetimeauth_state *mac_ctx;
  714. guchar RSPAMD_ALIGNED(32) subkey[CHACHA_BLOCKBYTES];
  715. mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT);
  716. memset (subkey, 0, sizeof (subkey));
  717. chacha_update (enc_ctx, subkey, subkey, sizeof (subkey));
  718. crypto_onetimeauth_init (mac_ctx, subkey);
  719. rspamd_explicit_memzero (subkey, sizeof (subkey));
  720. return mac_ctx;
  721. }
  722. else {
  723. #ifndef HAVE_USABLE_OPENSSL
  724. g_assert (0);
  725. #else
  726. auth_ctx = enc_ctx;
  727. return auth_ctx;
  728. #endif
  729. }
  730. return NULL;
  731. }
  732. static gboolean
  733. rspamd_cryptobox_decrypt_update (void *enc_ctx, const guchar *in, gsize inlen,
  734. guchar *out, gsize *outlen,
  735. enum rspamd_cryptobox_mode mode)
  736. {
  737. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  738. gsize r;
  739. chacha_state *s;
  740. s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT);
  741. r = chacha_update (s, in, out, inlen);
  742. if (outlen != NULL) {
  743. *outlen = r;
  744. }
  745. return TRUE;
  746. }
  747. else {
  748. #ifndef HAVE_USABLE_OPENSSL
  749. g_assert (0);
  750. #else
  751. EVP_CIPHER_CTX **s = enc_ctx;
  752. gint r;
  753. r = outlen ? *outlen : inlen;
  754. g_assert (EVP_DecryptUpdate (*s, out, &r, in, inlen) == 1);
  755. if (outlen) {
  756. *outlen = r;
  757. }
  758. return TRUE;
  759. #endif
  760. }
  761. }
  762. static gboolean
  763. rspamd_cryptobox_auth_verify_update (void *auth_ctx,
  764. const guchar *in, gsize inlen,
  765. enum rspamd_cryptobox_mode mode)
  766. {
  767. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  768. crypto_onetimeauth_state *mac_ctx;
  769. mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT);
  770. crypto_onetimeauth_update (mac_ctx, in, inlen);
  771. return TRUE;
  772. }
  773. else {
  774. #ifndef HAVE_USABLE_OPENSSL
  775. /* We do not need to authenticate as a separate process */
  776. return TRUE;
  777. #else
  778. #endif
  779. }
  780. return FALSE;
  781. }
  782. static gboolean
  783. rspamd_cryptobox_decrypt_final (void *enc_ctx, guchar *out, gsize remain,
  784. enum rspamd_cryptobox_mode mode)
  785. {
  786. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  787. chacha_state *s;
  788. s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT);
  789. chacha_final (s, out);
  790. return TRUE;
  791. }
  792. else {
  793. #ifndef HAVE_USABLE_OPENSSL
  794. g_assert (0);
  795. #else
  796. EVP_CIPHER_CTX **s = enc_ctx;
  797. gint r = remain;
  798. if (EVP_DecryptFinal_ex (*s, out, &r) < 0) {
  799. return FALSE;
  800. }
  801. return TRUE;
  802. #endif
  803. }
  804. return FALSE;
  805. }
  806. static gboolean
  807. rspamd_cryptobox_auth_verify_final (void *auth_ctx, const rspamd_mac_t sig,
  808. enum rspamd_cryptobox_mode mode)
  809. {
  810. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  811. rspamd_mac_t mac;
  812. crypto_onetimeauth_state *mac_ctx;
  813. mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT);
  814. crypto_onetimeauth_final (mac_ctx, mac);
  815. if (crypto_verify_16 (mac, sig) != 0) {
  816. return FALSE;
  817. }
  818. return TRUE;
  819. }
  820. else {
  821. #ifndef HAVE_USABLE_OPENSSL
  822. g_assert (0);
  823. #else
  824. EVP_CIPHER_CTX **s = auth_ctx;
  825. if (EVP_CIPHER_CTX_ctrl (*s, EVP_CTRL_GCM_SET_TAG, 16, (guchar *)sig) != 1) {
  826. return FALSE;
  827. }
  828. return TRUE;
  829. #endif
  830. }
  831. return FALSE;
  832. }
  833. static void
  834. rspamd_cryptobox_cleanup (void *enc_ctx, void *auth_ctx,
  835. enum rspamd_cryptobox_mode mode)
  836. {
  837. if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  838. crypto_onetimeauth_state *mac_ctx;
  839. mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT);
  840. rspamd_explicit_memzero (mac_ctx, sizeof (*mac_ctx));
  841. }
  842. else {
  843. #ifndef HAVE_USABLE_OPENSSL
  844. g_assert (0);
  845. #else
  846. EVP_CIPHER_CTX **s = enc_ctx;
  847. EVP_CIPHER_CTX_cleanup (*s);
  848. EVP_CIPHER_CTX_free (*s);
  849. #endif
  850. }
  851. }
  852. void rspamd_cryptobox_encrypt_nm_inplace (guchar *data, gsize len,
  853. const rspamd_nonce_t nonce,
  854. const rspamd_nm_t nm,
  855. rspamd_mac_t sig,
  856. enum rspamd_cryptobox_mode mode)
  857. {
  858. gsize r;
  859. void *enc_ctx, *auth_ctx;
  860. enc_ctx = g_alloca (rspamd_cryptobox_encrypt_ctx_len (mode));
  861. auth_ctx = g_alloca (rspamd_cryptobox_auth_ctx_len (mode));
  862. enc_ctx = rspamd_cryptobox_encrypt_init (enc_ctx, nonce, nm, mode);
  863. auth_ctx = rspamd_cryptobox_auth_init (auth_ctx, enc_ctx, mode);
  864. rspamd_cryptobox_encrypt_update (enc_ctx, data, len, data, &r, mode);
  865. rspamd_cryptobox_encrypt_final (enc_ctx, data + r, len - r, mode);
  866. rspamd_cryptobox_auth_update (auth_ctx, data, len, mode);
  867. rspamd_cryptobox_auth_final (auth_ctx, sig, mode);
  868. rspamd_cryptobox_cleanup (enc_ctx, auth_ctx, mode);
  869. }
  870. static void
  871. rspamd_cryptobox_flush_outbuf (struct rspamd_cryptobox_segment *st,
  872. const guchar *buf, gsize len, gsize offset)
  873. {
  874. gsize cpy_len;
  875. while (len > 0) {
  876. cpy_len = MIN (len, st->len - offset);
  877. memcpy (st->data + offset, buf, cpy_len);
  878. st ++;
  879. buf += cpy_len;
  880. len -= cpy_len;
  881. offset = 0;
  882. }
  883. }
  884. void
  885. rspamd_cryptobox_encryptv_nm_inplace (struct rspamd_cryptobox_segment *segments,
  886. gsize cnt,
  887. const rspamd_nonce_t nonce,
  888. const rspamd_nm_t nm, rspamd_mac_t sig,
  889. enum rspamd_cryptobox_mode mode)
  890. {
  891. struct rspamd_cryptobox_segment *cur = segments, *start_seg = segments;
  892. guchar outbuf[CHACHA_BLOCKBYTES * 16];
  893. void *enc_ctx, *auth_ctx;
  894. guchar *out, *in;
  895. gsize r, remain, inremain, seg_offset;
  896. enc_ctx = g_alloca (rspamd_cryptobox_encrypt_ctx_len (mode));
  897. auth_ctx = g_alloca (rspamd_cryptobox_auth_ctx_len (mode));
  898. enc_ctx = rspamd_cryptobox_encrypt_init (enc_ctx, nonce, nm, mode);
  899. auth_ctx = rspamd_cryptobox_auth_init (auth_ctx, enc_ctx, mode);
  900. remain = sizeof (outbuf);
  901. out = outbuf;
  902. inremain = cur->len;
  903. seg_offset = 0;
  904. for (;;) {
  905. if (cur - segments == (gint)cnt) {
  906. break;
  907. }
  908. if (cur->len <= remain) {
  909. memcpy (out, cur->data, cur->len);
  910. remain -= cur->len;
  911. out += cur->len;
  912. cur ++;
  913. if (remain == 0) {
  914. rspamd_cryptobox_encrypt_update (enc_ctx, outbuf, sizeof (outbuf),
  915. outbuf, NULL, mode);
  916. rspamd_cryptobox_auth_update (auth_ctx, outbuf, sizeof (outbuf),
  917. mode);
  918. rspamd_cryptobox_flush_outbuf (start_seg, outbuf,
  919. sizeof (outbuf), seg_offset);
  920. start_seg = cur;
  921. seg_offset = 0;
  922. remain = sizeof (outbuf);
  923. out = outbuf;
  924. }
  925. }
  926. else {
  927. memcpy (out, cur->data, remain);
  928. rspamd_cryptobox_encrypt_update (enc_ctx, outbuf, sizeof (outbuf),
  929. outbuf, NULL, mode);
  930. rspamd_cryptobox_auth_update (auth_ctx, outbuf, sizeof (outbuf),
  931. mode);
  932. rspamd_cryptobox_flush_outbuf (start_seg, outbuf, sizeof (outbuf),
  933. seg_offset);
  934. seg_offset = 0;
  935. inremain = cur->len - remain;
  936. in = cur->data + remain;
  937. out = outbuf;
  938. remain = 0;
  939. start_seg = cur;
  940. while (inremain > 0) {
  941. if (sizeof (outbuf) <= inremain) {
  942. memcpy (outbuf, in, sizeof (outbuf));
  943. rspamd_cryptobox_encrypt_update (enc_ctx,
  944. outbuf,
  945. sizeof (outbuf),
  946. outbuf,
  947. NULL,
  948. mode);
  949. rspamd_cryptobox_auth_update (auth_ctx,
  950. outbuf,
  951. sizeof (outbuf),
  952. mode);
  953. memcpy (in, outbuf, sizeof (outbuf));
  954. in += sizeof (outbuf);
  955. inremain -= sizeof (outbuf);
  956. remain = sizeof (outbuf);
  957. }
  958. else {
  959. memcpy (outbuf, in, inremain);
  960. remain = sizeof (outbuf) - inremain;
  961. out = outbuf + inremain;
  962. inremain = 0;
  963. }
  964. }
  965. seg_offset = cur->len - (sizeof (outbuf) - remain);
  966. cur ++;
  967. }
  968. }
  969. rspamd_cryptobox_encrypt_update (enc_ctx, outbuf, sizeof (outbuf) - remain,
  970. outbuf, &r, mode);
  971. out = outbuf + r;
  972. rspamd_cryptobox_encrypt_final (enc_ctx, out, sizeof (outbuf) - remain - r,
  973. mode);
  974. rspamd_cryptobox_auth_update (auth_ctx, outbuf, sizeof (outbuf) - remain,
  975. mode);
  976. rspamd_cryptobox_auth_final (auth_ctx, sig, mode);
  977. rspamd_cryptobox_flush_outbuf (start_seg, outbuf, sizeof (outbuf) - remain,
  978. seg_offset);
  979. rspamd_cryptobox_cleanup (enc_ctx, auth_ctx, mode);
  980. }
  981. gboolean
  982. rspamd_cryptobox_decrypt_nm_inplace (guchar *data, gsize len,
  983. const rspamd_nonce_t nonce, const rspamd_nm_t nm,
  984. const rspamd_mac_t sig, enum rspamd_cryptobox_mode mode)
  985. {
  986. gsize r = 0;
  987. gboolean ret = TRUE;
  988. void *enc_ctx, *auth_ctx;
  989. enc_ctx = g_alloca (rspamd_cryptobox_encrypt_ctx_len (mode));
  990. auth_ctx = g_alloca (rspamd_cryptobox_auth_ctx_len (mode));
  991. enc_ctx = rspamd_cryptobox_decrypt_init (enc_ctx, nonce, nm, mode);
  992. auth_ctx = rspamd_cryptobox_auth_verify_init (auth_ctx, enc_ctx, mode);
  993. rspamd_cryptobox_auth_verify_update (auth_ctx, data, len, mode);
  994. if (!rspamd_cryptobox_auth_verify_final (auth_ctx, sig, mode)) {
  995. ret = FALSE;
  996. }
  997. else {
  998. rspamd_cryptobox_decrypt_update (enc_ctx, data, len, data, &r, mode);
  999. ret = rspamd_cryptobox_decrypt_final (enc_ctx, data + r, len - r, mode);
  1000. }
  1001. rspamd_cryptobox_cleanup (enc_ctx, auth_ctx, mode);
  1002. return ret;
  1003. }
  1004. gboolean
  1005. rspamd_cryptobox_decrypt_inplace (guchar *data, gsize len,
  1006. const rspamd_nonce_t nonce,
  1007. const rspamd_pk_t pk, const rspamd_sk_t sk,
  1008. const rspamd_mac_t sig,
  1009. enum rspamd_cryptobox_mode mode)
  1010. {
  1011. guchar nm[rspamd_cryptobox_MAX_NMBYTES];
  1012. gboolean ret;
  1013. rspamd_cryptobox_nm (nm, pk, sk, mode);
  1014. ret = rspamd_cryptobox_decrypt_nm_inplace (data, len, nonce, nm, sig, mode);
  1015. rspamd_explicit_memzero (nm, sizeof (nm));
  1016. return ret;
  1017. }
  1018. void
  1019. rspamd_cryptobox_encrypt_inplace (guchar *data, gsize len,
  1020. const rspamd_nonce_t nonce,
  1021. const rspamd_pk_t pk, const rspamd_sk_t sk,
  1022. rspamd_mac_t sig,
  1023. enum rspamd_cryptobox_mode mode)
  1024. {
  1025. guchar nm[rspamd_cryptobox_MAX_NMBYTES];
  1026. rspamd_cryptobox_nm (nm, pk, sk, mode);
  1027. rspamd_cryptobox_encrypt_nm_inplace (data, len, nonce, nm, sig, mode);
  1028. rspamd_explicit_memzero (nm, sizeof (nm));
  1029. }
  1030. void
  1031. rspamd_cryptobox_encryptv_inplace (struct rspamd_cryptobox_segment *segments,
  1032. gsize cnt,
  1033. const rspamd_nonce_t nonce,
  1034. const rspamd_pk_t pk, const rspamd_sk_t sk,
  1035. rspamd_mac_t sig,
  1036. enum rspamd_cryptobox_mode mode)
  1037. {
  1038. guchar nm[rspamd_cryptobox_MAX_NMBYTES];
  1039. rspamd_cryptobox_nm (nm, pk, sk, mode);
  1040. rspamd_cryptobox_encryptv_nm_inplace (segments, cnt, nonce, nm, sig, mode);
  1041. rspamd_explicit_memzero (nm, sizeof (nm));
  1042. }
  1043. void
  1044. rspamd_cryptobox_siphash (unsigned char *out, const unsigned char *in,
  1045. unsigned long long inlen,
  1046. const rspamd_sipkey_t k)
  1047. {
  1048. crypto_shorthash_siphash24 (out, in, inlen, k);
  1049. }
  1050. /*
  1051. * Password-Based Key Derivation Function 2 (PKCS #5 v2.0).
  1052. * Code based on IEEE Std 802.11-2007, Annex H.4.2.
  1053. */
  1054. static gboolean
  1055. rspamd_cryptobox_pbkdf2 (const char *pass, gsize pass_len,
  1056. const guint8 *salt, gsize salt_len, guint8 *key, gsize key_len,
  1057. unsigned int rounds)
  1058. {
  1059. guint8 *asalt, obuf[crypto_generichash_blake2b_BYTES_MAX];
  1060. guint8 d1[crypto_generichash_blake2b_BYTES_MAX],
  1061. d2[crypto_generichash_blake2b_BYTES_MAX];
  1062. unsigned int i, j;
  1063. unsigned int count;
  1064. gsize r;
  1065. if (rounds < 1 || key_len == 0) {
  1066. return FALSE;
  1067. }
  1068. if (salt_len == 0 || salt_len > G_MAXSIZE - 4) {
  1069. return FALSE;
  1070. }
  1071. asalt = g_malloc (salt_len + 4);
  1072. memcpy (asalt, salt, salt_len);
  1073. for (count = 1; key_len > 0; count++) {
  1074. asalt[salt_len + 0] = (count >> 24) & 0xff;
  1075. asalt[salt_len + 1] = (count >> 16) & 0xff;
  1076. asalt[salt_len + 2] = (count >> 8) & 0xff;
  1077. asalt[salt_len + 3] = count & 0xff;
  1078. if (pass_len <= crypto_generichash_blake2b_KEYBYTES_MAX) {
  1079. crypto_generichash_blake2b (d1, sizeof (d1), asalt, salt_len + 4,
  1080. pass, pass_len);
  1081. }
  1082. else {
  1083. guint8 k[crypto_generichash_blake2b_BYTES_MAX];
  1084. /*
  1085. * We use additional blake2 iteration to store large key
  1086. * XXX: it is not compatible with the original implementation but safe
  1087. */
  1088. crypto_generichash_blake2b (k, sizeof (k), pass, pass_len,
  1089. NULL, 0);
  1090. crypto_generichash_blake2b (d1, sizeof (d1), asalt, salt_len + 4,
  1091. k, sizeof (k));
  1092. }
  1093. memcpy (obuf, d1, sizeof(obuf));
  1094. for (i = 1; i < rounds; i++) {
  1095. if (pass_len <= crypto_generichash_blake2b_KEYBYTES_MAX) {
  1096. crypto_generichash_blake2b (d2, sizeof (d2), d1, sizeof (d1),
  1097. pass, pass_len);
  1098. }
  1099. else {
  1100. guint8 k[crypto_generichash_blake2b_BYTES_MAX];
  1101. /*
  1102. * We use additional blake2 iteration to store large key
  1103. * XXX: it is not compatible with the original implementation but safe
  1104. */
  1105. crypto_generichash_blake2b (k, sizeof (k), pass, pass_len,
  1106. NULL, 0);
  1107. crypto_generichash_blake2b (d2, sizeof (d2), d1, sizeof (d1),
  1108. k, sizeof (k));
  1109. }
  1110. memcpy (d1, d2, sizeof(d1));
  1111. for (j = 0; j < sizeof(obuf); j++) {
  1112. obuf[j] ^= d1[j];
  1113. }
  1114. }
  1115. r = MIN(key_len, crypto_generichash_blake2b_BYTES_MAX);
  1116. memcpy (key, obuf, r);
  1117. key += r;
  1118. key_len -= r;
  1119. }
  1120. rspamd_explicit_memzero (asalt, salt_len + 4);
  1121. g_free (asalt);
  1122. rspamd_explicit_memzero (d1, sizeof (d1));
  1123. rspamd_explicit_memzero (d2, sizeof (d2));
  1124. rspamd_explicit_memzero (obuf, sizeof (obuf));
  1125. return TRUE;
  1126. }
  1127. gboolean
  1128. rspamd_cryptobox_pbkdf (const char *pass, gsize pass_len,
  1129. const guint8 *salt, gsize salt_len, guint8 *key, gsize key_len,
  1130. unsigned int complexity, enum rspamd_cryptobox_pbkdf_type type)
  1131. {
  1132. gboolean ret = FALSE;
  1133. switch (type) {
  1134. case RSPAMD_CRYPTOBOX_CATENA:
  1135. if (catena (pass, pass_len, salt, salt_len, "rspamd", 6,
  1136. 4, complexity, complexity, key_len, key) == 0) {
  1137. ret = TRUE;
  1138. }
  1139. break;
  1140. case RSPAMD_CRYPTOBOX_PBKDF2:
  1141. default:
  1142. ret = rspamd_cryptobox_pbkdf2 (pass, pass_len, salt, salt_len, key,
  1143. key_len, complexity);
  1144. break;
  1145. }
  1146. return ret;
  1147. }
  1148. guint
  1149. rspamd_cryptobox_pk_bytes (enum rspamd_cryptobox_mode mode)
  1150. {
  1151. if (G_UNLIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  1152. return 32;
  1153. }
  1154. else {
  1155. return 65;
  1156. }
  1157. }
  1158. guint
  1159. rspamd_cryptobox_pk_sig_bytes (enum rspamd_cryptobox_mode mode)
  1160. {
  1161. if (G_UNLIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  1162. return 32;
  1163. }
  1164. else {
  1165. return 65;
  1166. }
  1167. }
  1168. guint
  1169. rspamd_cryptobox_nonce_bytes (enum rspamd_cryptobox_mode mode)
  1170. {
  1171. if (G_UNLIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  1172. return 24;
  1173. }
  1174. else {
  1175. return 16;
  1176. }
  1177. }
  1178. guint
  1179. rspamd_cryptobox_sk_bytes (enum rspamd_cryptobox_mode mode)
  1180. {
  1181. return 32;
  1182. }
  1183. guint
  1184. rspamd_cryptobox_sk_sig_bytes (enum rspamd_cryptobox_mode mode)
  1185. {
  1186. if (G_UNLIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  1187. return 64;
  1188. }
  1189. else {
  1190. return 32;
  1191. }
  1192. }
  1193. guint
  1194. rspamd_cryptobox_signature_bytes (enum rspamd_cryptobox_mode mode)
  1195. {
  1196. static guint ssl_keylen;
  1197. if (G_UNLIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) {
  1198. return 64;
  1199. }
  1200. else {
  1201. #ifndef HAVE_USABLE_OPENSSL
  1202. g_assert (0);
  1203. #else
  1204. if (ssl_keylen == 0) {
  1205. EC_KEY *lk;
  1206. lk = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID);
  1207. ssl_keylen = ECDSA_size (lk);
  1208. EC_KEY_free (lk);
  1209. }
  1210. #endif
  1211. return ssl_keylen;
  1212. }
  1213. }
  1214. guint
  1215. rspamd_cryptobox_nm_bytes (enum rspamd_cryptobox_mode mode)
  1216. {
  1217. return 32;
  1218. }
  1219. guint
  1220. rspamd_cryptobox_mac_bytes (enum rspamd_cryptobox_mode mode)
  1221. {
  1222. return 16;
  1223. }
  1224. void
  1225. rspamd_cryptobox_hash_init (rspamd_cryptobox_hash_state_t *p, const guchar *key, gsize keylen)
  1226. {
  1227. crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
  1228. RSPAMD_ALIGNOF(crypto_generichash_blake2b_state));
  1229. crypto_generichash_blake2b_init (st, key, keylen,
  1230. crypto_generichash_blake2b_BYTES_MAX);
  1231. }
  1232. /**
  1233. * Update hash with data portion
  1234. */
  1235. void
  1236. rspamd_cryptobox_hash_update (rspamd_cryptobox_hash_state_t *p, const guchar *data, gsize len)
  1237. {
  1238. crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
  1239. RSPAMD_ALIGNOF(crypto_generichash_blake2b_state));
  1240. crypto_generichash_blake2b_update (st, data, len);
  1241. }
  1242. /**
  1243. * Output hash to the buffer of rspamd_cryptobox_HASHBYTES length
  1244. */
  1245. void
  1246. rspamd_cryptobox_hash_final (rspamd_cryptobox_hash_state_t *p, guchar *out)
  1247. {
  1248. crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
  1249. RSPAMD_ALIGNOF(crypto_generichash_blake2b_state));
  1250. crypto_generichash_blake2b_final (st, out, crypto_generichash_blake2b_BYTES_MAX);
  1251. }
  1252. /**
  1253. * One in all function
  1254. */
  1255. void rspamd_cryptobox_hash (guchar *out,
  1256. const guchar *data,
  1257. gsize len,
  1258. const guchar *key,
  1259. gsize keylen)
  1260. {
  1261. crypto_generichash_blake2b (out, crypto_generichash_blake2b_BYTES_MAX,
  1262. data, len, key, keylen);
  1263. }
  1264. G_STATIC_ASSERT (sizeof (t1ha_context_t) <=
  1265. sizeof (((rspamd_cryptobox_fast_hash_state_t *)NULL)->opaque));
  1266. G_STATIC_ASSERT (sizeof (struct XXH3_state_s) <=
  1267. sizeof (((rspamd_cryptobox_fast_hash_state_t *)NULL)->opaque));
  1268. struct RSPAMD_ALIGNED(16) _mum_iuf {
  1269. union {
  1270. gint64 ll;
  1271. unsigned char b[sizeof (guint64)];
  1272. } buf;
  1273. gint64 h;
  1274. unsigned rem;
  1275. };
  1276. rspamd_cryptobox_fast_hash_state_t*
  1277. rspamd_cryptobox_fast_hash_new(void)
  1278. {
  1279. rspamd_cryptobox_fast_hash_state_t *nst;
  1280. int ret = posix_memalign ((void **)&nst, RSPAMD_ALIGNOF(rspamd_cryptobox_fast_hash_state_t),
  1281. sizeof(rspamd_cryptobox_fast_hash_state_t));
  1282. if (ret != 0) {
  1283. abort();
  1284. }
  1285. return nst;
  1286. }
  1287. void
  1288. rspamd_cryptobox_fast_hash_free(rspamd_cryptobox_fast_hash_state_t *st)
  1289. {
  1290. free(st);
  1291. }
  1292. void
  1293. rspamd_cryptobox_fast_hash_init (rspamd_cryptobox_fast_hash_state_t *st,
  1294. guint64 seed)
  1295. {
  1296. XXH3_state_t *rst = (XXH3_state_t *)st->opaque;
  1297. st->type = RSPAMD_CRYPTOBOX_XXHASH3;
  1298. XXH3_64bits_reset_withSeed (rst, seed);
  1299. }
  1300. void
  1301. rspamd_cryptobox_fast_hash_init_specific (rspamd_cryptobox_fast_hash_state_t *st,
  1302. enum rspamd_cryptobox_fast_hash_type type,
  1303. guint64 seed)
  1304. {
  1305. switch (type) {
  1306. case RSPAMD_CRYPTOBOX_T1HA:
  1307. case RSPAMD_CRYPTOBOX_HASHFAST:
  1308. case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT: {
  1309. t1ha_context_t *rst = (t1ha_context_t *) st->opaque;
  1310. st->type = RSPAMD_CRYPTOBOX_T1HA;
  1311. t1ha2_init (rst, seed, 0);
  1312. break;
  1313. }
  1314. case RSPAMD_CRYPTOBOX_XXHASH64: {
  1315. XXH64_state_t *xst = (XXH64_state_t *) st->opaque;
  1316. st->type = RSPAMD_CRYPTOBOX_XXHASH64;
  1317. XXH64_reset (xst, seed);
  1318. break;
  1319. }
  1320. case RSPAMD_CRYPTOBOX_XXHASH32:
  1321. {
  1322. XXH32_state_t *xst = (XXH32_state_t *) st->opaque;
  1323. st->type = RSPAMD_CRYPTOBOX_XXHASH32;
  1324. XXH32_reset (xst, seed);
  1325. break;
  1326. }
  1327. case RSPAMD_CRYPTOBOX_XXHASH3:
  1328. {
  1329. XXH3_state_t *xst = (XXH3_state_t *) st->opaque;
  1330. st->type = RSPAMD_CRYPTOBOX_XXHASH3;
  1331. XXH3_64bits_reset_withSeed (xst, seed);
  1332. break;
  1333. }
  1334. case RSPAMD_CRYPTOBOX_MUMHASH: {
  1335. struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque;
  1336. st->type = RSPAMD_CRYPTOBOX_MUMHASH;
  1337. iuf->h = seed;
  1338. iuf->buf.ll = 0;
  1339. iuf->rem = 0;
  1340. break;
  1341. }
  1342. }
  1343. }
  1344. void
  1345. rspamd_cryptobox_fast_hash_update (rspamd_cryptobox_fast_hash_state_t *st,
  1346. const void *data, gsize len)
  1347. {
  1348. if (st->type == RSPAMD_CRYPTOBOX_T1HA) {
  1349. t1ha_context_t *rst = (t1ha_context_t *) st->opaque;
  1350. t1ha2_update (rst, data, len);
  1351. }
  1352. else {
  1353. switch (st->type) {
  1354. case RSPAMD_CRYPTOBOX_XXHASH64: {
  1355. XXH64_state_t *xst = (XXH64_state_t *) st->opaque;
  1356. XXH64_update (xst, data, len);
  1357. break;
  1358. }
  1359. case RSPAMD_CRYPTOBOX_XXHASH32:
  1360. {
  1361. XXH32_state_t *xst = (XXH32_state_t *) st->opaque;
  1362. XXH32_update (xst, data, len);
  1363. break;
  1364. }
  1365. case RSPAMD_CRYPTOBOX_XXHASH3:
  1366. {
  1367. XXH3_state_t *xst = (XXH3_state_t *) st->opaque;
  1368. XXH3_64bits_update (xst, data, len);
  1369. break;
  1370. }
  1371. case RSPAMD_CRYPTOBOX_MUMHASH: {
  1372. struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque;
  1373. gsize drem = len;
  1374. const guchar *p = data;
  1375. if (iuf->rem > 0) {
  1376. /* Process remainder */
  1377. if (drem >= iuf->rem) {
  1378. memcpy (iuf->buf.b + sizeof (iuf->buf.ll) - iuf->rem,
  1379. p, iuf->rem);
  1380. drem -= iuf->rem;
  1381. p += iuf->rem;
  1382. iuf->h = mum_hash_step (iuf->h, iuf->buf.ll);
  1383. iuf->rem = 0;
  1384. }
  1385. else {
  1386. memcpy (iuf->buf.b + sizeof (iuf->buf.ll) - iuf->rem, p, drem);
  1387. iuf->rem -= drem;
  1388. drem = 0;
  1389. }
  1390. }
  1391. while (drem >= sizeof (iuf->buf.ll)) {
  1392. memcpy (iuf->buf.b, p, sizeof (iuf->buf.ll));
  1393. iuf->h = mum_hash_step (iuf->h, iuf->buf.ll);
  1394. drem -= sizeof (iuf->buf.ll);
  1395. p += sizeof (iuf->buf.ll);
  1396. }
  1397. /* Leftover */
  1398. if (drem > 0) {
  1399. iuf->rem = sizeof (guint64) - drem;
  1400. iuf->buf.ll = 0;
  1401. memcpy (iuf->buf.b, p, drem);
  1402. }
  1403. break;
  1404. }
  1405. case RSPAMD_CRYPTOBOX_T1HA:
  1406. case RSPAMD_CRYPTOBOX_HASHFAST:
  1407. case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT: {
  1408. t1ha_context_t *rst = (t1ha_context_t *) st->opaque;
  1409. t1ha2_update (rst, data, len);
  1410. break;
  1411. }
  1412. }
  1413. }
  1414. }
  1415. guint64
  1416. rspamd_cryptobox_fast_hash_final (rspamd_cryptobox_fast_hash_state_t *st)
  1417. {
  1418. guint64 ret;
  1419. if (st->type == RSPAMD_CRYPTOBOX_T1HA) {
  1420. t1ha_context_t *rst = (t1ha_context_t *) st->opaque;
  1421. return t1ha2_final (rst, NULL);
  1422. }
  1423. else {
  1424. switch (st->type) {
  1425. case RSPAMD_CRYPTOBOX_XXHASH64: {
  1426. XXH64_state_t *xst = (XXH64_state_t *) st->opaque;
  1427. ret = XXH64_digest (xst);
  1428. break;
  1429. }
  1430. case RSPAMD_CRYPTOBOX_XXHASH32: {
  1431. XXH32_state_t *xst = (XXH32_state_t *) st->opaque;
  1432. ret = XXH32_digest (xst);
  1433. break;
  1434. }
  1435. case RSPAMD_CRYPTOBOX_XXHASH3: {
  1436. XXH3_state_t *xst = (XXH3_state_t *) st->opaque;
  1437. ret = XXH3_64bits_digest (xst);
  1438. break;
  1439. }
  1440. case RSPAMD_CRYPTOBOX_MUMHASH: {
  1441. struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque;
  1442. iuf->h = mum_hash_step (iuf->h, iuf->buf.ll);
  1443. ret = mum_hash_finish (iuf->h);
  1444. break;
  1445. }
  1446. case RSPAMD_CRYPTOBOX_T1HA:
  1447. case RSPAMD_CRYPTOBOX_HASHFAST:
  1448. case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT: {
  1449. t1ha_context_t *rst = (t1ha_context_t *) st->opaque;
  1450. ret = t1ha2_final (rst, NULL);
  1451. break;
  1452. }
  1453. }
  1454. }
  1455. return ret;
  1456. }
  1457. /**
  1458. * One in all function
  1459. */
  1460. static inline guint64
  1461. rspamd_cryptobox_fast_hash_machdep (const void *data,
  1462. gsize len, guint64 seed)
  1463. {
  1464. return XXH3_64bits_withSeed(data, len, seed);
  1465. }
  1466. static inline guint64
  1467. rspamd_cryptobox_fast_hash_indep (const void *data,
  1468. gsize len, guint64 seed)
  1469. {
  1470. return XXH3_64bits_withSeed(data, len, seed);
  1471. }
  1472. guint64
  1473. rspamd_cryptobox_fast_hash (const void *data,
  1474. gsize len, guint64 seed)
  1475. {
  1476. return rspamd_cryptobox_fast_hash_machdep (data, len, seed);
  1477. }
  1478. guint64
  1479. rspamd_cryptobox_fast_hash_specific (
  1480. enum rspamd_cryptobox_fast_hash_type type,
  1481. const void *data,
  1482. gsize len, guint64 seed)
  1483. {
  1484. switch (type) {
  1485. case RSPAMD_CRYPTOBOX_XXHASH32:
  1486. return XXH32 (data, len, seed);
  1487. case RSPAMD_CRYPTOBOX_XXHASH3:
  1488. return XXH3_64bits_withSeed (data, len, seed);
  1489. case RSPAMD_CRYPTOBOX_XXHASH64:
  1490. return XXH64 (data, len, seed);
  1491. case RSPAMD_CRYPTOBOX_MUMHASH:
  1492. return mum_hash (data, len, seed);
  1493. case RSPAMD_CRYPTOBOX_T1HA:
  1494. return t1ha2_atonce (data, len, seed);
  1495. case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT:
  1496. return rspamd_cryptobox_fast_hash_indep (data, len, seed);
  1497. case RSPAMD_CRYPTOBOX_HASHFAST:
  1498. default:
  1499. return rspamd_cryptobox_fast_hash_machdep (data, len, seed);
  1500. }
  1501. }