// File: /workspace/github_cpp_makefile_star2_O3/dguglielmi[P]dlan-linux-software/common/hptools.cpp
// Function: HomePlugAvTools::DeviceAccessKeyFromSecurityID(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned char*)
void HomePlugAvTools::DeviceAccessKeyFromSecurityID(
  const std::string& strSecurityID,
  unsigned char cDAK[16])
{
  std::string strSaltedSecret = strSecurityID.substr(0, 64) + std::string("\x08\x85\x6D\xAF\x7C\xF5\x81\x85", 8);
  
  sha256_state sha256;
  unsigned char digest[32];
  sha256_init(&sha256);
  sha256_process(&sha256, (const unsigned char*)strSaltedSecret.data(), strSaltedSecret.size());
  sha256_done(&sha256, digest);
  for(int i = 0; i < 999; i++) 
  {
    sha256_init(&sha256);
    sha256_process(&sha256, digest, 32);
    sha256_done(&sha256, digest);
  }
  
  memcpy(cDAK, digest, 16);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %rbx
movq %rdi, %rsi
leaq 0x48(%rsp), %r15
movl $0x40, %ecx
movq %r15, %rdi
xorl %edx, %edx
callq 0x4290
leaq 0x30(%rsp), %r14
movq %r14, -0x10(%r14)
movabsq $-0x7a7e0a8350927af8, %rax # imm = 0x8581F57CAF6D8508
movq %rax, (%r14)
movq $0x8, -0x8(%r14)
movb $0x0, 0x8(%r14)
movq (%r15), %rcx
movq 0x8(%r15), %r8
leaq 0x8(%r8), %rax
leaq 0x58(%rsp), %r15
movl $0xf, %edx
cmpq %r15, %rcx
je 0x10f81
movq 0x58(%rsp), %rdx
cmpq $0xf, %rax
ja 0x10f9c
cmpq %rdx, %rax
jbe 0x10f9c
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x42b0
jmp 0x10fae
leaq 0x48(%rsp), %rdi
movl $0x8, %edx
movq %r14, %rsi
callq 0x40e0
leaq 0x10(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
je 0x10fd1
movq %rsi, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0x10fd7
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x1100b
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x11022
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4270
leaq 0x48(%rsp), %r14
movl $0x0, 0x28(%r14)
movq $0x0, (%r14)
movaps 0x56b3(%rip), %xmm0     # 0x166f0
movups %xmm0, 0x8(%r14)
movaps 0x56b7(%rip), %xmm0     # 0x16700
movups %xmm0, 0x18(%r14)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rdi
callq 0x10d9b
leaq 0x20(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x10e4c
movl $0x3e7, %ebp            # imm = 0x3E7
movl $0x0, 0x70(%rsp)
movq $0x0, 0x48(%rsp)
movaps 0x5664(%rip), %xmm0     # 0x166f0
movups %xmm0, 0x50(%rsp)
movaps 0x5668(%rip), %xmm0     # 0x16700
movups %xmm0, 0x60(%rsp)
movl $0x20, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x10d9b
movq %r14, %rdi
movq %r15, %rsi
callq 0x10e4c
decl %ebp
jne 0x11074
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rbx)
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x110df
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x4270
addq $0xb8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x11107
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x1111e
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x43e0