Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argon2 format: Custom malloc/free using callbacks #5566

Merged
merged 3 commits into from
Nov 7, 2024

Conversation

magnumripper
Copy link
Member

We never free memory except in format's done(), instead we reuse the allocations per thread, and realloc if needed.

We also got rid of a small malloc/free per call for a temporary buffer, by moving away from argon2_hash() in favor of argon2_ctx().

Closes #5558

Comment on lines 288 to 302
static void deallocate(uint8_t *memory, size_t size)
{
if (!thread_mem[THREAD_NUMBER].used)
error_msg("%s(): thread %u: Freed memory not in use\n", __FUNCTION__, THREAD_NUMBER);
if (thread_mem[THREAD_NUMBER].size < size)
error_msg("%s(): thread %u: incorrect size %zu, was %zu\n", __FUNCTION__, THREAD_NUMBER, size, thread_mem[THREAD_NUMBER].size);

thread_mem[THREAD_NUMBER].used = 0;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why they supply a size to deallocate() but I used it for an extra check, bc why not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe can even insist on == size in that check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it might be smaller after we reused the existing alloc for a smaller request.

@magnumripper
Copy link
Member Author

magnumripper commented Nov 7, 2024

Well that crashed and burned. I'll be trying to understand what ASan wants to explain to me.

Edit: OK that was a fencepost error for non-OMP. Thread was hard coded as 1 instead of 0.

@magnumripper
Copy link
Member Author

@solardiz if this PR is correct, I can go ahead and switch to alloc_region_t() instead - either as a separate commit or as a fixup here.

Copy link
Member

@solardiz solardiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I've made a couple of suggestions for a fixup of the existing commit.

Please do also switch to using the "regions" functions, but in a separate commit. Thank you!

Comment on lines 288 to 302
static void deallocate(uint8_t *memory, size_t size)
{
if (!thread_mem[THREAD_NUMBER].used)
error_msg("%s(): thread %u: Freed memory not in use\n", __FUNCTION__, THREAD_NUMBER);
if (thread_mem[THREAD_NUMBER].size < size)
error_msg("%s(): thread %u: incorrect size %zu, was %zu\n", __FUNCTION__, THREAD_NUMBER, size, thread_mem[THREAD_NUMBER].size);

thread_mem[THREAD_NUMBER].used = 0;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe can even insist on == size in that check?

context.allocate_cbk = &allocate;
context.free_cbk = &deallocate;
context.flags = ARGON2_DEFAULT_FLAGS;
context.version = ARGON2_VERSION_NUMBER;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use struct initialized with named fields here, which is a C feature that we already use in the tree anyway (and for a related purpose). Like this:

argon2_context context = {
        .out = (uint8_t*)crypted[i],
and so on
};

omitting the fields where we initialize to NULL or 0. That way, we'll be sure to initialize the entire struct even if more fields are added later.

Our existing usage is e.g.:

yescrypt_params_t params = {.N = cur_salt->N, .r = cur_salt->r, .p = cur_salt->p};

We never free memory except in format's done(), instead we reuse the
allocations per thread, and realloc if needed.

We also got rid of a small malloc/free per call for a temporary buffer,
by moving away from argon2_hash() in favor of argon2_ctx().

Closes openwall#5558
@solardiz
Copy link
Member

solardiz commented Nov 7, 2024

The switch to regions looks good to me, except that our size field became redundant with what's inside the region struct - maybe switch to reusing the latter?

@solardiz
Copy link
Member

solardiz commented Nov 7, 2024

e.g. yescrypt does:

                if (local->aligned_size < need) {
                        if (free_region(local))
                                return -1;
                        if (!alloc_region(local, need))
                                return -1;
                }

without needing to maintain a separate size field.

@solardiz
Copy link
Member

solardiz commented Nov 7, 2024

and in the Armory format I did:

        lut_item *lut = memory->aligned;
        size_t bytes_reqd = (size_t)n * sizeof(*lut);
        if (!lut || memory->aligned_size < bytes_reqd) {
                free_region_t(memory);
                if (!(lut = alloc_region_t(memory, bytes_reqd)))
                        return -1;
        }

@magnumripper
Copy link
Member Author

magnumripper commented Nov 7, 2024

The switch to regions looks good to me, except that our size field became redundant with what's inside the region struct - maybe switch to reusing the latter?

Ah, yes. The fact we can hand out an oversized existing alloc had me confused but right now they are indeed always the same value.

I can throw in a commit resetting MKPC and OMP_SCALE to 1 as well, while at it.

@solardiz
Copy link
Member

solardiz commented Nov 7, 2024

I can throw in a commit resetting MKPC and OMP_SCALE to 1 as well, while at it.

That's OK with me.

@solardiz solardiz merged commit 6c182c1 into openwall:bleeding-jumbo Nov 7, 2024
35 of 36 checks passed
@magnumripper magnumripper deleted the argon2-callback branch November 7, 2024 03:53
@solardiz
Copy link
Member

solardiz commented Nov 7, 2024

We were ignoring possible error returns from Argon2 code before this PR and made it even worse with this PR. Memory allocation errors go undetected, resulting in "high speeds". I'm going to fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Argon2: Reintroduce the memory (de)allocation optimization
2 participants