// File: /workspace/github_cpp_makefile_star2_O3/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 0x40a619
movq %r9, %r14
testq %rdx, %rdx
cmoveq %rdx, %rcx
leaq 0xe7587(%rip), %rsi     # 0x4f1a5d
cmovneq %rdx, %rsi
shrq $0x5, %r9
movl %r14d, %edx
andl $0x1f, %edx
cmpq $0x1, %rdx
sbbq $-0x1, %r9
movq %r9, 0x8(%rsp)
cmpq $0xff, %r9
ja 0x40a619
movq %rdi, 0x30(%rsp)
leaq 0x80(%rsp), %rdi
movl $0x118, %edx            # imm = 0x118
movq %rsi, 0x28(%rsp)
xorl %esi, %esi
movq %rcx, 0x20(%rsp)
movq %r8, 0x18(%rsp)
callq 0x401098
cmpq $0x0, 0x8(%rsp)
je 0x40a5ea
movq 0x8(%rsp), %rax
decq %rax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %r14, 0x38(%rsp)
leaq 0x1(%rbx), %r12
movb %r12b, 0x7(%rsp)
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %rdx
xorl %ecx, %ecx
callq 0x409f6f
movq %r14, %rdi
leaq 0x40(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rdx
callq 0x405697
movq %r14, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x405697
movl $0x1, %edx
movq %r14, %rdi
leaq 0x7(%rsp), %rsi
callq 0x405697
movq %r14, %rdi
movq 0x38(%rsp), %r14
movq %rbp, %rsi
callq 0x40a0e1
cmpq %rbx, 0x10(%rsp)
movl $0x20, %r13d
movl $0x20, %edx
cmoveq %r14, %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x401040
addq $-0x20, %r14
addq $0x20, %r15
movq %r12, %rbx
cmpq %r12, 0x8(%rsp)
jne 0x40a544
movq 0x103e27(%rip), %rax    # 0x50e418
leaq 0x80(%rsp), %rdi
movl $0x118, %edx            # imm = 0x118
xorl %esi, %esi
callq *%rax
movq 0x103e0f(%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