// File: 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:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x1d8(%rsp)
movq %rsi, 0x1d0(%rsp)
movq %rdx, 0x1c8(%rsp)
movq %rcx, 0x1c0(%rsp)
movq %r8, 0x1b8(%rsp)
movq %r9, 0x1b0(%rsp)
movq $0x0, 0x1a0(%rsp)
movq $0x0, 0x190(%rsp)
movl $0x0, 0x184(%rsp)
cmpq $0x0, 0x1b8(%rsp)
jne 0x4115e5
movl $0xffffa080, 0x1e4(%rsp) # imm = 0xFFFFA080
jmp 0x41182d
movq $0x20, 0x1a8(%rsp)
movq 0x1d0(%rsp), %rax
cmpq 0x1a8(%rsp), %rax
jb 0x41160e
cmpq $0x0, 0x1a8(%rsp)
jne 0x41161e
movl $0xffffa080, 0x1e4(%rsp) # imm = 0xFFFFA080
jmp 0x41182d
cmpq $0x0, 0x1c8(%rsp)
jne 0x411644
leaq 0xe734d(%rip), %rax     # 0x4f897d
movq %rax, 0x1c8(%rsp)
movq $0x0, 0x1c0(%rsp)
movq 0x1b0(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x1a8(%rsp)
movq %rax, 0x198(%rsp)
movq 0x1b0(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x1a8(%rsp)
cmpq $0x0, %rdx
je 0x41168e
movq 0x198(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x198(%rsp)
cmpq $0xff, 0x198(%rsp)
jbe 0x4116ac
movl $0xffffa080, 0x1e4(%rsp) # imm = 0xFFFFA080
jmp 0x41182d
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
movl $0x118, %edx            # imm = 0x118
callq 0x401098
movq $0x1, 0x188(%rsp)
movq 0x188(%rsp), %rax
cmpq 0x198(%rsp), %rax
ja 0x411801
movq 0x188(%rsp), %rax
andq $0xff, %rax
movb %al, 0x17(%rsp)
movq 0x1d8(%rsp), %rsi
movq 0x1d0(%rsp), %rdx
leaq 0x68(%rsp), %rdi
xorl %ecx, %ecx
callq 0x410ed0
leaq 0x20(%rsp), %rsi
movq 0x190(%rsp), %rdx
leaq 0x68(%rsp), %rdi
callq 0x411030
movq 0x1c8(%rsp), %rsi
movq 0x1c0(%rsp), %rdx
leaq 0x68(%rsp), %rdi
callq 0x411030
leaq 0x68(%rsp), %rdi
leaq 0x17(%rsp), %rsi
movl $0x1, %edx
callq 0x411030
leaq 0x20(%rsp), %rsi
leaq 0x68(%rsp), %rdi
callq 0x411060
movq 0x188(%rsp), %rax
cmpq 0x198(%rsp), %rax
je 0x411782
movq 0x1a8(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x411797
movq 0x1b0(%rsp), %rax
subq 0x1a0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x1b8(%rsp), %rdi
addq 0x1a0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x401040
movq 0x1a8(%rsp), %rax
addq 0x1a0(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movq 0x1a8(%rsp), %rax
movq %rax, 0x190(%rsp)
movq 0x188(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x188(%rsp)
jmp 0x4116c9
leaq 0x68(%rsp), %rdi
movl $0x118, %esi            # imm = 0x118
callq 0x411490
leaq 0x20(%rsp), %rdi
movl $0x40, %esi
callq 0x411490
movl 0x184(%rsp), %eax
movl %eax, 0x1e4(%rsp)
movl 0x1e4(%rsp), %eax
addq $0x1e8, %rsp            # imm = 0x1E8
retq
nopl (%rax)