// File: /workspace/github_cpp_makefile_star2_O1/wangyu-[P]pwdgen/lib/pbkdf2-sha1.cpp
// Function: PKCS5_PBKDF2_HMAC_SHA1(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned long, unsigned long, unsigned char*)
void PKCS5_PBKDF2_HMAC_SHA1(const unsigned char *password, size_t plen,
    const unsigned char *salt, size_t slen,
    const unsigned long iteration_count, const unsigned long key_length,
    unsigned char *output)
{
	sha1_context ctx;
	sha1_starts(&ctx);

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

	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;

		sha1_hmac_starts(&ctx, password, plen);
		sha1_hmac_update(&ctx, salt, slen);
		sha1_hmac_update(&ctx, c, 4);
		sha1_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
			sha1_hmac_starts(&ctx, password, plen);
			sha1_hmac_update(&ctx, md1, md_size);
			sha1_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 $0x178, %rsp            # imm = 0x178
movq %rcx, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x67452301, 0x90(%rsp) # imm = 0x67452301
movl $0x98badcfe, %eax       # imm = 0x98BADCFE
leaq 0x5712ce8b(%rax), %rcx
movq %rcx, 0x98(%rsp)
movq %rax, 0xa0(%rsp)
movq $0x10325476, 0xa8(%rsp) # imm = 0x10325476
addq $0x2b1804f2, %rax       # imm = 0x2B1804F2
movq %rax, 0xb0(%rsp)
movq %r9, 0x18(%rsp)
testq %r9, %r9
je 0x4055be
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
movl %r13d, %edx
callq 0x405227
movq %r15, %rdi
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x403429
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x403429
movq %r15, %rdi
movq %r12, %rsi
callq 0x40530e
movl 0x70(%rsp), %eax
movl %eax, 0x50(%rsp)
movaps 0x60(%rsp), %xmm0
movaps %xmm0, 0x40(%rsp)
cmpq $0x2, %r14
jb 0x40556b
movl $0x1, %ebx
movq %r15, %rdi
movq %rbp, %rsi
movl %r13d, %edx
callq 0x405227
movq %r15, %rdi
movq %r12, %rsi
movl $0x14, %edx
callq 0x403429
movq %r15, %rdi
movq %r12, %rsi
callq 0x40530e
xorl %eax, %eax
movb 0x60(%rsp,%rax), %cl
xorb %cl, 0x40(%rsp,%rax)
incq %rax
cmpq $0x14, %rax
jne 0x405552
incq %rbx
cmpq %r14, %rbx
jne 0x405527
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r14
subq %r14, %rbx
cmpq $0x14, %rbx
movl $0x14, %eax
cmovaeq %rax, %rbx
movq 0x1b0(%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 0x4054c2
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq