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 39KB

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