// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x40(%rsp)
testq %r8, %r8
sete %al
movq %rsi, 0x48(%rsp)
cmpq $0x20, %rsi
setb %sil
orb %al, %sil
movl $0xffffa080, %r15d      # imm = 0xFFFFA080
jne 0x40a2d3
testq %rdx, %rdx
cmoveq %rdx, %rcx
leaq 0xe788b(%rip), %rax     # 0x4f1a1d
cmovneq %rdx, %rax
movq %rax, 0x38(%rsp)
movq %r9, %rdx
shrq $0x5, %rdx
movl %r9d, %eax
andl $0x1f, %eax
cmpq $0x1, %rax
sbbq $-0x1, %rdx
cmpq $0xff, %rdx
ja 0x40a2d3
movq %r8, 0x18(%rsp)
movq %r9, 0x20(%rsp)
movq %rcx, 0x30(%rsp)
leaq 0x90(%rsp), %r13
xorl %ebp, %ebp
movq %rdx, 0x10(%rsp)
movl $0x118, %edx            # imm = 0x118
movq %r13, %rdi
xorl %esi, %esi
callq 0x401098
movq 0x10(%rsp), %rax
leaq -0x1(%rax), %rcx
movq %rcx, 0x28(%rsp)
xorl %r14d, %r14d
cmpq %rbp, %rax
je 0x40a2a5
leaq 0x1(%rbp), %r15
movb %r15b, 0xf(%rsp)
movq %r13, %rdi
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
xorl %ecx, %ecx
callq 0x409daf
movq %r13, %rdi
leaq 0x50(%rsp), %rbx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4054cf
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x4054cf
movq %r13, %rdi
leaq 0xf(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x4054cf
movq %r13, %rdi
movq %rbx, %rsi
callq 0x409e79
cmpq %rbp, 0x28(%rsp)
pushq $0x20
popq %r14
movq %r14, %rdx
movq 0x20(%rsp), %r12
cmoveq %r12, %rdx
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x401040
movq 0x10(%rsp), %rax
addq $0x20, %rbp
movq %rbp, 0x18(%rsp)
addq $-0x20, %r12
movq %r12, 0x20(%rsp)
movq %r15, %rbp
jmp 0x40a1fb
movq 0x10416c(%rip), %rax    # 0x50e418
leaq 0x90(%rsp), %rdi
xorl %r15d, %r15d
movl $0x118, %edx            # imm = 0x118
xorl %esi, %esi
callq *%rax
movq 0x104151(%rip), %rax    # 0x50e418
leaq 0x50(%rsp), %rdi
pushq $0x40
popq %rdx
xorl %esi, %esi
callq *%rax
movl %r15d, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq