// File: /workspace/github_cpp_makefile_star2_O1/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 %rcx, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x6a09e667, 0x90(%rsp) # imm = 0x6A09E667
movl $0x9b05688c, %eax       # imm = 0x9B05688C
leaq 0x206245f9(%rax), %rcx
movq %rcx, 0x98(%rsp)
movq $0x3c6ef372, 0xa0(%rsp) # imm = 0x3C6EF372
leaq 0xa4a8cae(%rax), %rcx
movq %rcx, 0xa8(%rsp)
movq $0x510e527f, 0xb0(%rsp) # imm = 0x510E527F
movq %rax, 0xb8(%rsp)
movq $0x1f83d9ab, 0xc0(%rsp) # imm = 0x1F83D9AB
movq $0x5be0cd19, 0xc8(%rsp) # imm = 0x5BE0CD19
movl $0x0, 0x190(%rsp)
movq %r9, 0x18(%rsp)
testq %r9, %r9
je 0x40a388
movq %r8, %r14
movq %rsi, %r13
movq %rdi, %rbp
movl $0x1, %eax
movq $0x0, 0x10(%rsp)
leaq 0x80(%rsp), %r15
leaq 0x60(%rsp), %r12
movq %r8, 0x20(%rsp)
movq %rax, 0x38(%rsp)
bswapl %eax
movl %eax, 0xc(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x409fdf
movq %r15, %rdi
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x405686
movl $0x4, %edx
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
callq 0x405686
movq %r15, %rdi
movq %r12, %rsi
callq 0x40a0b3
movaps 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
movaps %xmm1, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
cmpq $0x2, %r14
jb 0x40a335
movl $0x1, %ebx
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x409fdf
movl $0x20, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x405686
movq %r15, %rdi
movq %r12, %rsi
callq 0x40a0b3
xorl %eax, %eax
movaps 0x40(%rsp,%rax), %xmm0
xorps 0x60(%rsp,%rax), %xmm0
movaps %xmm0, 0x40(%rsp,%rax)
addq $0x10, %rax
cmpq $0x20, %rax
jne 0x40a314
incq %rbx
cmpq %r14, %rbx
jne 0x40a2e7
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r14
subq %r14, %rbx
cmpq $0x20, %rbx
movl $0x20, %eax
cmovaeq %rax, %rbx
movq 0x1d0(%rsp), %rax
leaq (%rax,%r14), %rdi
leaq 0x40(%rsp), %rsi
movq %rbx, %rdx
callq 0x401040
addq %rbx, %r14
movq 0x38(%rsp), %rax
incq %rax
movq %r14, 0x10(%rsp)
cmpq 0x18(%rsp), %r14
movq 0x20(%rsp), %r14
jb 0x40a27e
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq