Opened 18 months ago

Last modified 15 months ago

#18062 new enhancement

Implement crypt_r in libroot

Reported by: davidkaroly Owned by: nobody
Priority: normal Milestone: Unscheduled
Component: - General Version: R1/beta3
Keywords: Cc:
Blocked By: Blocking:
Platform: All

Description

We have crypt() implemented in libroot/posix/crypt/crypt.cpp but it uses a statically defined outBuffer.

I think it would make it easier to port existing software to Haiku if we implement crypt_r() as well, and probably it won't be too difficult - unless I overlooked something.

Was there any particular reason for not implementing crypt_r() or is it only that it was not needed yet?

Change History (7)

comment:1 by waddlesplash, 18 months ago

Probably it just wasn't needed yet. What actually uses crypt_r?

comment:2 by davidkaroly, 18 months ago

I stumbled on this while investigating if filezilla could be ported to Haiku.

Anyway I see now that the UFC implementation in crypt_legacy.c and crypt_legacy_util.c uses a bunch of statics and global variables.

comment:3 by davidkaroly, 18 months ago

something strange in crypto_scrypt.cpp - it seems that some code was adapted from Tarsnap scrypt library but only part of it was used:

static void (*smix_func)(uint8_t *, size_t, uint64_t, void *, void *) = NULL;

static void
selectsmix(void)
{
        /* If generic smix works, use it. */
        if (!testsmix(crypto_scrypt_smix)) {
                smix_func = crypto_scrypt_smix;
                return;
        }

        /* If we get here, something really bad happened. */
        abort();
}

Here we are trying to select the optimal crypto_scrypt implementation but only the reference implementation is available, no optimized version. (upstream also has an SSE2 optimized version here)

So we end up with additional complexity (global variable etc) but no performance gain. Perhaps this code path could be simplified?

comment:4 by davidkaroly, 17 months ago

I think we should also replace UFC crypt with MUSL crypt_des. Maybe better than what glibc did with _ufc_doit_r or FreeBSD declaring a bunch of threadlocal variables.

see:

comment:5 by waddlesplash, 17 months ago

Note that our passwd files use this "crypt" to hash login passwords, so any change will have to preserve backwards compatibility for at least some time.

comment:6 by davidkaroly, 17 months ago

Yep, understood.

I prepared a first draft that attempts to make scrypt reentrant. I'd like to upload it for review in the coming days.

CryptTest still passes - even though I'm not sure how much test coverage we have.

comment:7 by davidkaroly, 15 months ago

most of it has been merged:

  • hrev56758 - libroot: introduce crypt_r
  • hrev56766 - libroot/crypt: replace crypt_des implementation with musl's
  • hrev56771 - libroot/crypt: remove selectsmix

with these in place, crypt_r now seems threadsafe enough.

I have a small test program that invokes crypt_r on multiple threads, recently it hasn't found any issues. I'll try to integrate it into the unit tests located in haiku repo src/tests/ folder.

Note: See TracTickets for help on using tickets.