// File: /workspace/github_cpp_makefile_star2_O1/wangyu-[P]pwdgen/lib/pbkdf2-sha256.cpp
// Function: hkdf_sha256_expand(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned char*, unsigned long)
int hkdf_sha256_expand( const unsigned char *prk,
                         size_t prk_len, const unsigned char *info,
                         size_t info_len, unsigned char *okm, size_t okm_len )
{
    size_t hash_len;
    size_t where = 0;
    size_t n;
    size_t t_len = 0;
    size_t i;
    int ret = 0;
    sha2_context ctx;
    unsigned char t[MBEDTLS_MD_MAX_SIZE];

    if( okm == NULL )
    {
        return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
    }

    hash_len = sha256_len;

    if( prk_len < hash_len || hash_len == 0 )
    {
        return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
    }

    if( info == NULL )
    {
        info = (const unsigned char *) "";
        info_len = 0;
    }

    n = okm_len / hash_len;

    if( (okm_len % hash_len) != 0 )
    {
        n++;
    }

    if( n > 255 )
    {
        return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
    }

    //mbedtls_md_init( &ctx );   //old code
    memset( &ctx, 0, sizeof( ctx) );  //its not necessary

    /*
    if( (ret = mbedtls_md_setup( &ctx, md, 1) ) != 0 )
    {
        goto exit;
    }*/


    /* RFC 5869 Section 2.3. */
    for( i = 1; i <= n; i++ )
    {
        size_t num_to_copy;
        unsigned char c = i & 0xff;

        sha2_hmac_starts( &ctx, prk, prk_len,0 );

        sha2_hmac_update( &ctx, t, t_len );

        sha2_hmac_update( &ctx, info, info_len );

        /* The constant concatenated to the end of each t(n) is a single octet.
         * */
        sha2_hmac_update( &ctx, &c, 1 );

        sha2_hmac_finish( &ctx, t );
        num_to_copy = i != n ? hash_len : okm_len - where;
        memcpy( okm + where, t, num_to_copy );
        where += hash_len;
        t_len = hash_len;
    }

//exit:
    //mbedtls_md_free( &ctx );  //old code
    mbedtls_platform_zeroize( &ctx, sizeof( ctx ) ); //not necessary too

    mbedtls_platform_zeroize( t, sizeof( t ) );

    return( ret );
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %r8, %r15
movq %rsi, %r8
testq %r15, %r15
sete %al
cmpq $0x20, %rsi
setb %sil
orb %al, %sil
movl $0xffffa080, %eax       # imm = 0xFFFFA080
jne 0x40a565
movq %r9, %r14
testq %rdx, %rdx
cmoveq %rdx, %rcx
leaq 0xe75b8(%rip), %rsi     # 0x4f19dd
cmovneq %rdx, %rsi
movq %r9, %rbx
shrq $0x5, %rbx
xorl %r12d, %r12d
testb $0x1f, %r14b
setne %r12b
leaq (%rbx,%r12), %r13
cmpq $0xff, %r13
ja 0x40a565
movq %rdi, 0x38(%rsp)
leaq 0x80(%rsp), %rdi
movl $0x118, %edx            # imm = 0x118
movq %rsi, 0x30(%rsp)
xorl %esi, %esi
movq %rcx, 0x28(%rsp)
movq %r8, 0x20(%rsp)
callq 0x401098
testq %r13, %r13
je 0x40a536
movq %r13, %rax
leaq (%rbx,%r12), %rcx
decq %rcx
movq %rcx, 0x18(%rsp)
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %rax, 0x10(%rsp)
leaq 0x1(%rbx), %r12
movb %r12b, 0xf(%rsp)
leaq 0x80(%rsp), %rbp
movq %rbp, %rdi
movq 0x38(%rsp), %rsi
movq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0x409fdf
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
movq %r13, %rdx
callq 0x405686
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x405686
movl $0x1, %edx
movq %rbp, %rdi
leaq 0xf(%rsp), %rsi
callq 0x405686
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x40a0b3
cmpq %rbx, 0x18(%rsp)
movl $0x20, %r13d
movl $0x20, %edx
cmoveq %r14, %rdx
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x401040
movq 0x10(%rsp), %rax
addq $-0x20, %r14
addq $0x20, %r15
movq %r12, %rbx
cmpq %r12, %rax
jne 0x40a496
movq 0x103edb(%rip), %rax    # 0x50e418
leaq 0x80(%rsp), %rdi
movl $0x118, %edx            # imm = 0x118
xorl %esi, %esi
callq *%rax
movq 0x103ec3(%rip), %rax    # 0x50e418
leaq 0x40(%rsp), %rdi
movl $0x40, %edx
xorl %esi, %esi
callq *%rax
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq