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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  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 eax, ecx = 0, ebx = 0, 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. #ifdef HAVE_SSE2
  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. #ifdef HAVE_SSE42
  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 (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 (void *p, const guchar *key, gsize keylen)
  1224. {
  1225. if (key != NULL && keylen > 0) {
  1226. crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
  1227. _Alignof(crypto_generichash_blake2b_state));
  1228. crypto_generichash_blake2b_init (st, key, keylen,
  1229. crypto_generichash_blake2b_BYTES_MAX);
  1230. }
  1231. else {
  1232. crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
  1233. _Alignof(crypto_generichash_blake2b_state));
  1234. crypto_generichash_blake2b_init (st, key, keylen,
  1235. crypto_generichash_blake2b_BYTES_MAX);
  1236. }
  1237. }
  1238. /**
  1239. * Update hash with data portion
  1240. */
  1241. void
  1242. rspamd_cryptobox_hash_update (void *p, const guchar *data, gsize len)
  1243. {
  1244. crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
  1245. _Alignof(crypto_generichash_blake2b_state));
  1246. crypto_generichash_blake2b_update (st, data, len);
  1247. }
  1248. /**
  1249. * Output hash to the buffer of rspamd_cryptobox_HASHBYTES length
  1250. */
  1251. void
  1252. rspamd_cryptobox_hash_final (void *p, guchar *out)
  1253. {
  1254. crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
  1255. _Alignof(crypto_generichash_blake2b_state));
  1256. crypto_generichash_blake2b_final (st, out, crypto_generichash_blake2b_BYTES_MAX);
  1257. }
  1258. /**
  1259. * One in all function
  1260. */
  1261. void rspamd_cryptobox_hash (guchar *out,
  1262. const guchar *data,
  1263. gsize len,
  1264. const guchar *key,
  1265. gsize keylen)
  1266. {
  1267. crypto_generichash_blake2b (out, crypto_generichash_blake2b_BYTES_MAX,
  1268. data, len, key, keylen);
  1269. }
  1270. G_STATIC_ASSERT (sizeof (t1ha_context_t) <=
  1271. sizeof (((rspamd_cryptobox_fast_hash_state_t *)NULL)->opaque));
  1272. G_STATIC_ASSERT (sizeof (XXH64_state_t) <=
  1273. sizeof (((rspamd_cryptobox_fast_hash_state_t *)NULL)->opaque));
  1274. struct RSPAMD_ALIGNED(16) _mum_iuf {
  1275. union {
  1276. gint64 ll;
  1277. unsigned char b[sizeof (guint64)];
  1278. } buf;
  1279. gint64 h;
  1280. unsigned rem;
  1281. };
  1282. void
  1283. rspamd_cryptobox_fast_hash_init (rspamd_cryptobox_fast_hash_state_t *st,
  1284. guint64 seed)
  1285. {
  1286. t1ha_context_t *rst = (t1ha_context_t *)st->opaque;
  1287. st->type = RSPAMD_CRYPTOBOX_T1HA;
  1288. t1ha2_init (rst, seed, 0);
  1289. }
  1290. void
  1291. rspamd_cryptobox_fast_hash_init_specific (rspamd_cryptobox_fast_hash_state_t *st,
  1292. enum rspamd_cryptobox_fast_hash_type type,
  1293. guint64 seed)
  1294. {
  1295. switch (type) {
  1296. case RSPAMD_CRYPTOBOX_T1HA:
  1297. case RSPAMD_CRYPTOBOX_HASHFAST:
  1298. case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT: {
  1299. t1ha_context_t *rst = (t1ha_context_t *) st->opaque;
  1300. st->type = RSPAMD_CRYPTOBOX_T1HA;
  1301. t1ha2_init (rst, seed, 0);
  1302. break;
  1303. }
  1304. case RSPAMD_CRYPTOBOX_XXHASH64: {
  1305. XXH64_state_t *xst = (XXH64_state_t *) st->opaque;
  1306. st->type = RSPAMD_CRYPTOBOX_XXHASH64;
  1307. XXH64_reset (xst, seed);
  1308. break;
  1309. }
  1310. case RSPAMD_CRYPTOBOX_XXHASH32:
  1311. {
  1312. XXH32_state_t *xst = (XXH32_state_t *) st->opaque;
  1313. st->type = RSPAMD_CRYPTOBOX_XXHASH32;
  1314. XXH32_reset (xst, seed);
  1315. break;
  1316. }
  1317. case RSPAMD_CRYPTOBOX_MUMHASH: {
  1318. struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque;
  1319. st->type = RSPAMD_CRYPTOBOX_MUMHASH;
  1320. iuf->h = seed;
  1321. iuf->buf.ll = 0;
  1322. iuf->rem = 0;
  1323. break;
  1324. }
  1325. }
  1326. }
  1327. void
  1328. rspamd_cryptobox_fast_hash_update (rspamd_cryptobox_fast_hash_state_t *st,
  1329. const void *data, gsize len)
  1330. {
  1331. if (G_LIKELY (st->type) == RSPAMD_CRYPTOBOX_T1HA) {
  1332. t1ha_context_t *rst = (t1ha_context_t *) st->opaque;
  1333. t1ha2_update (rst, data, len);
  1334. }
  1335. else {
  1336. switch (st->type) {
  1337. case RSPAMD_CRYPTOBOX_XXHASH64: {
  1338. XXH64_state_t *xst = (XXH64_state_t *) st->opaque;
  1339. XXH64_update (xst, data, len);
  1340. break;
  1341. }
  1342. case RSPAMD_CRYPTOBOX_XXHASH32:
  1343. {
  1344. XXH32_state_t *xst = (XXH32_state_t *) st->opaque;
  1345. XXH32_update (xst, data, len);
  1346. break;
  1347. }
  1348. case RSPAMD_CRYPTOBOX_MUMHASH: {
  1349. struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque;
  1350. gsize drem = len;
  1351. const guchar *p = data;
  1352. if (iuf->rem > 0) {
  1353. /* Process remainder */
  1354. if (drem >= iuf->rem) {
  1355. memcpy (iuf->buf.b + sizeof (iuf->buf.ll) - iuf->rem,
  1356. p, iuf->rem);
  1357. drem -= iuf->rem;
  1358. p += iuf->rem;
  1359. iuf->h = mum_hash_step (iuf->h, iuf->buf.ll);
  1360. iuf->rem = 0;
  1361. }
  1362. else {
  1363. memcpy (iuf->buf.b + sizeof (iuf->buf.ll) - iuf->rem, p, drem);
  1364. iuf->rem -= drem;
  1365. drem = 0;
  1366. }
  1367. }
  1368. while (drem >= sizeof (iuf->buf.ll)) {
  1369. memcpy (iuf->buf.b, p, sizeof (iuf->buf.ll));
  1370. iuf->h = mum_hash_step (iuf->h, iuf->buf.ll);
  1371. drem -= sizeof (iuf->buf.ll);
  1372. p += sizeof (iuf->buf.ll);
  1373. }
  1374. /* Leftover */
  1375. if (drem > 0) {
  1376. iuf->rem = sizeof (guint64) - drem;
  1377. iuf->buf.ll = 0;
  1378. memcpy (iuf->buf.b, p, drem);
  1379. }
  1380. break;
  1381. }
  1382. case RSPAMD_CRYPTOBOX_T1HA:
  1383. case RSPAMD_CRYPTOBOX_HASHFAST:
  1384. case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT: {
  1385. t1ha_context_t *rst = (t1ha_context_t *) st->opaque;
  1386. t1ha2_update (rst, data, len);
  1387. break;
  1388. }
  1389. }
  1390. }
  1391. }
  1392. guint64
  1393. rspamd_cryptobox_fast_hash_final (rspamd_cryptobox_fast_hash_state_t *st)
  1394. {
  1395. guint64 ret;
  1396. if (G_LIKELY (st->type) == RSPAMD_CRYPTOBOX_T1HA) {
  1397. t1ha_context_t *rst = (t1ha_context_t *) st->opaque;
  1398. return t1ha2_final (rst, NULL);
  1399. }
  1400. else {
  1401. switch (st->type) {
  1402. case RSPAMD_CRYPTOBOX_XXHASH64: {
  1403. XXH64_state_t *xst = (XXH64_state_t *) st->opaque;
  1404. ret = XXH64_digest (xst);
  1405. break;
  1406. }
  1407. case RSPAMD_CRYPTOBOX_XXHASH32: {
  1408. XXH32_state_t *xst = (XXH32_state_t *) st->opaque;
  1409. ret = XXH32_digest (xst);
  1410. break;
  1411. }
  1412. case RSPAMD_CRYPTOBOX_MUMHASH: {
  1413. struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque;
  1414. iuf->h = mum_hash_step (iuf->h, iuf->buf.ll);
  1415. ret = mum_hash_finish (iuf->h);
  1416. break;
  1417. }
  1418. case RSPAMD_CRYPTOBOX_T1HA:
  1419. case RSPAMD_CRYPTOBOX_HASHFAST:
  1420. case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT: {
  1421. t1ha_context_t *rst = (t1ha_context_t *) st->opaque;
  1422. ret = t1ha2_final (rst, NULL);
  1423. break;
  1424. }
  1425. }
  1426. }
  1427. return ret;
  1428. }
  1429. /**
  1430. * One in all function
  1431. */
  1432. static inline guint64
  1433. rspamd_cryptobox_fast_hash_machdep (const void *data,
  1434. gsize len, guint64 seed)
  1435. {
  1436. return t1ha2_atonce (data, len, seed);
  1437. }
  1438. static inline guint64
  1439. rspamd_cryptobox_fast_hash_indep (const void *data,
  1440. gsize len, guint64 seed)
  1441. {
  1442. return t1ha2_atonce (data, len, seed);
  1443. }
  1444. guint64
  1445. rspamd_cryptobox_fast_hash (const void *data,
  1446. gsize len, guint64 seed)
  1447. {
  1448. return rspamd_cryptobox_fast_hash_machdep (data, len, seed);
  1449. }
  1450. guint64
  1451. rspamd_cryptobox_fast_hash_specific (
  1452. enum rspamd_cryptobox_fast_hash_type type,
  1453. const void *data,
  1454. gsize len, guint64 seed)
  1455. {
  1456. switch (type) {
  1457. case RSPAMD_CRYPTOBOX_XXHASH32:
  1458. return XXH32 (data, len, seed);
  1459. case RSPAMD_CRYPTOBOX_XXHASH64:
  1460. return XXH64 (data, len, seed);
  1461. case RSPAMD_CRYPTOBOX_MUMHASH:
  1462. return mum_hash (data, len, seed);
  1463. case RSPAMD_CRYPTOBOX_T1HA:
  1464. return t1ha2_atonce (data, len, seed);
  1465. case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT:
  1466. return rspamd_cryptobox_fast_hash_indep (data, len, seed);
  1467. case RSPAMD_CRYPTOBOX_HASHFAST:
  1468. default:
  1469. return rspamd_cryptobox_fast_hash_machdep (data, len, seed);
  1470. }
  1471. }