// File: /workspace/github_cpp_makefile_star2_O2/wangyu-[P]pwdgen/lib/pbkdf2-sha256.cpp
// Function: PKCS5_PBKDF2_HMAC_SHA256(unsigned char*, unsigned long, unsigned char*, unsigned long, unsigned long, unsigned long, unsigned char*)
void PKCS5_PBKDF2_HMAC_SHA256(unsigned char *password, size_t plen,
    unsigned char *salt, size_t slen,
    const unsigned long iteration_count, const unsigned long key_length,
    unsigned char *output)
{
	sha2_context ctx;
	sha2_starts(&ctx, 0);

	// Size of the generated digest
	unsigned char md_size = 32;
	unsigned char md1[32];
	unsigned char work[32];

	unsigned long counter = 1;
	unsigned long generated_key_length = 0;
	while (generated_key_length < key_length) {
		// U1 ends up in md1 and work
		unsigned char c[4];
		c[0] = (counter >> 24) & 0xff;
		c[1] = (counter >> 16) & 0xff;
		c[2] = (counter >> 8) & 0xff;
		c[3] = (counter >> 0) & 0xff;

		sha2_hmac_starts(&ctx, password, plen, 0);
		sha2_hmac_update(&ctx, salt, slen);
		sha2_hmac_update(&ctx, c, 4);
		sha2_hmac_finish(&ctx, md1);
		memcpy(work, md1, md_size);

		unsigned long ic = 1;
		for (ic = 1; ic < iteration_count; ic++) {
			// U2 ends up in md1
			sha2_hmac_starts(&ctx, password, plen, 0);
			sha2_hmac_update(&ctx, md1, md_size);
			sha2_hmac_finish(&ctx, md1);
			// U1 xor U2
			unsigned long i = 0;
			for (i = 0; i < md_size; i++) {
				work[i] ^= md1[i];
			}
			// and so on until iteration_count
		}

		// Copy the generated bytes to the key
		unsigned long bytes_to_write =
		    min((key_length - generated_key_length), md_size);
		memcpy(output + generated_key_length, work, bytes_to_write);
		generated_key_length += bytes_to_write;
		++counter;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %r9, 0x28(%rsp)
movq %r8, %r14
movq %rcx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r13
movq %rdi, %rbp
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %r15
movaps %xmm0, (%r15)
movq $0x6a09e667, 0x10(%r15) # imm = 0x6A09E667
movl $0x9b05688c, %eax       # imm = 0x9B05688C
leaq 0x206245f9(%rax), %rcx
movq %rcx, 0x18(%r15)
movq $0x3c6ef372, 0x20(%r15) # imm = 0x3C6EF372
leaq 0xa4a8cae(%rax), %rcx
movq %rcx, 0x28(%r15)
movq $0x510e527f, 0x30(%r15) # imm = 0x510E527F
movq %rax, 0x38(%r15)
movq $0x1f83d9ab, 0x40(%r15) # imm = 0x1F83D9AB
movq $0x5be0cd19, 0x48(%r15) # imm = 0x5BE0CD19
andl $0x0, 0x110(%r15)
pushq $0x1
popq %rax
xorl %edx, %edx
leaq 0x60(%rsp), %r12
movq 0x28(%rsp), %rcx
subq %rdx, %rcx
movq %rcx, 0x38(%rsp)
jbe 0x40a0f5
movq %rdx, 0x10(%rsp)
movq %rax, 0x30(%rsp)
bswapl %eax
movl %eax, 0xc(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x409daf
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x4054cf
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x4054cf
movq %r15, %rdi
movq %r12, %rsi
callq 0x409e79
movaps 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
movaps %xmm1, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
pushq $0x1
popq %rbx
cmpq %r14, %rbx
jae 0x40a0b2
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x409daf
movq %r15, %rdi
movq %r12, %rsi
pushq $0x20
popq %rdx
callq 0x4054cf
movq %r15, %rdi
movq %r12, %rsi
callq 0x409e79
xorl %eax, %eax
cmpq $0x20, %rax
je 0x40a0ad
movb 0x60(%rsp,%rax), %cl
xorb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x40a09a
incq %rbx
jmp 0x40a06a
movq 0x38(%rsp), %rbx
cmpq $0x20, %rbx
pushq $0x20
popq %rax
cmovaeq %rax, %rbx
movq 0x1d0(%rsp), %rax
movq 0x10(%rsp), %rcx
leaq (%rax,%rcx), %rdi
leaq 0x40(%rsp), %rsi
movq %rbx, %rdx
callq 0x401040
movq 0x10(%rsp), %rdx
addq %rbx, %rdx
movq 0x30(%rsp), %rax
incq %rax
jmp 0x409ff3
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq