// File: /workspace/github_cpp_makefile_star2_O1/FFY00[P]uicc/src/program_uicc.c
// Function: authenticate(char*, uicc_vals&)
void authenticate(char *port, struct uicc_vals &values) {
    string key;
    Assert(makeBin(values.key, key) == 16, "can't read a correct key: 16 hexa figures\n");
    string opc;
    Assert(makeBin(values.opc, opc) == 16, "can't read a correct opc: 16 hexa figures\n");
    USIM USIMcard;
    string ATR;
    Assert((ATR=USIMcard.open(port))!="", "Failed to open %s", port);
    //dump_hex("ATR", ATR);
    USIMcard.openUSIM();
    USIMcard.debug=false;
    // We don't make proper values for rand, sqn,
    // we perform first authentication only to get the AUTS from the USIM
    u8 amf[2]= {0};
    u8 sqn[6]= {0};
    u8 rand[16]= {0};
    u8 autn[16]= {0};
    u8 ik[16]= {0};
    u8 ck[16]= {0};
    u8 res[8]= {0};
    Assert(milenage_generate((const uint8_t *)opc.c_str(), amf,
                             (const uint8_t *)key.c_str(), sqn,
                             rand,
                             autn, ik, ck, res),
           "Milenage internal failure\n");
    string srand((char *)rand,sizeof(rand));
    string sautn((char *)autn,sizeof(autn));
    vector<string> returned=USIMcard.authenticate(srand, sautn);

    // We should have one LV value returned, the AUTS
    if (returned.size()!=1) {
        printf("The card didn't accept our challenge: OPc or Ki is wrong\n");
        return;
    }

    u8 SIMsqn[8]= {0};

    if ( ! milenage_auts((const uint8_t *)opc.c_str(),
                         (const uint8_t *)key.c_str(),
                         rand,
                         (const uint8_t *)returned[0].c_str(),
                         SIMsqn+2) ) {
        printf("Can't decode the AUTS returned by the card (wrong Ki or OPc)\n");
        return;
    }

    uint64_t intSqn=be64toh(*(uint64_t *)SIMsqn);
    intSqn+=32; // according to 3GPP TS 33.102 version 11, annex C. 3.2
    uint64_t newSqn=htobe64(intSqn);
    // To make better validation, let's generate a random value in milenage "rand"
    FILE *h=fopen("/dev/random","r");
    fread(rand,sizeof(rand),1,h);
    fclose(h);
    Assert( milenage_generate((const uint8_t *)opc.c_str(), amf,
                              (const uint8_t *)key.c_str(),
                              ((u8 *)&newSqn)+2,
                              rand,
                              autn, ik, ck, res),
            "Milenage internal failure\n");
    // Now, we can test a authentication that should be sucessful
    string new_autn((char *)autn,16);
    string new_rand((char *)rand,16);
    vector<string>  returned_newSQN=USIMcard.authenticate(new_rand, new_autn);

    if ( USIMcard.debug )
        for (size_t i=0; i< returned_newSQN.size(); i++)
            dump_hex("auth answer",returned_newSQN[i]);

    if (returned_newSQN.size() != 4)
        printf("We tried SQN %" PRId64 ", but the card refused!\n",intSqn);
    else {
        string s_ik((char *)ik,sizeof(ik));
        string s_ck((char *)ck,sizeof(ck));
        string s_res((char *)res,sizeof(res));

        if ( s_res != returned_newSQN[0] ||
                s_ck  != returned_newSQN[1] ||
                s_ik  != returned_newSQN[2] )
            printf("The card sent back vectors, but they are not our milenage computation\n");
        else {
            printf("Succeeded to authentify with SQN: %" PRId64 "\n", intSqn);
            printf("set HSS SQN value as: %" PRId64 "\n", intSqn+32 );
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x338, %rsp            # imm = 0x338
movq %rsi, %r14
movq %rdi, %rbx
leaq -0x120(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x310(%rbp), %r12
movq %r12, -0x10(%r12)
movq 0xe8(%rsi), %rsi
movq 0xf0(%r14), %rdx
addq %rsi, %rdx
leaq -0x320(%rbp), %rdi
callq 0x1372a
leaq -0x320(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x924f
movl %eax, %r15d
movq -0x320(%rbp), %rdi
cmpq %r12, %rdi
je 0x93cf
callq 0x2220
cmpl $0x10, %r15d
jne 0x9cf3
leaq -0x100(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x2f0(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x68(%r14), %rsi
movq 0x70(%r14), %rdx
addq %rsi, %rdx
leaq -0x300(%rbp), %rdi
callq 0x1372a
leaq -0x300(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x924f
movl %eax, %r14d
movq -0x300(%rbp), %rdi
cmpq %r15, %rdi
je 0x9438
callq 0x2220
cmpl $0x10, %r14d
jne 0x9cf8
movb $0x0, -0x220(%rbp)
movl $0xffffffff, -0x21c(%rbp) # imm = 0xFFFFFFFF
leaq 0xefa6(%rip), %rdi      # 0x18400
callq 0x2310
testq %rax, %rax
je 0x9476
movzbl (%rax), %eax
orl $0x20, %eax
cmpl $0x79, %eax
jne 0x9476
movb $0x1, -0x220(%rbp)
leaq -0x208(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x1e8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x160(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x88(%rbp), %rdi
leaq -0x220(%rbp), %rsi
movq %rbx, %rdx
callq 0xa812
leaq -0x170(%rbp), %rdi
leaq -0x88(%rbp), %r15
movq %r15, %rsi
callq 0x2320
leaq 0xeed4(%rip), %rsi      # 0x183b2
movq %rax, %rdi
callq 0x20b0
movl %eax, %r14d
movq (%r15), %rdi
leaq -0x78(%rbp), %r15
cmpq %r15, %rdi
je 0x94fa
callq 0x2220
testl %r14d, %r14d
je 0x9cfd
leaq -0x220(%rbp), %rdi
callq 0xb842
movb $0x0, -0x220(%rbp)
xorl %eax, %eax
leaq -0x2a(%rbp), %rsi
movw %ax, (%rsi)
leaq -0x260(%rbp), %rcx
movw %ax, 0x4(%rcx)
movl $0x0, (%rcx)
xorps %xmm0, %xmm0
leaq -0x190(%rbp), %r8
movaps %xmm0, (%r8)
leaq -0x180(%rbp), %r9
movaps %xmm0, (%r9)
leaq -0x340(%rbp), %r12
movaps %xmm0, (%r12)
leaq -0x330(%rbp), %r13
movaps %xmm0, (%r13)
leaq -0x1b0(%rbp), %rbx
movq $0x0, (%rbx)
movq -0x110(%rbp), %rdi
movq -0x130(%rbp), %rdx
subq $0x8, %rsp
pushq %rbx
pushq %r13
pushq %r12
callq 0x454e
addq $0x20, %rsp
leaq -0x88(%rbp), %rdi
movq %r15, (%rdi)
leaq -0xc8(%rbp), %rsi
movq $0x10, (%rsi)
xorl %edx, %edx
callq 0x2420
movq %rax, -0x88(%rbp)
leaq -0xb8(%rbp), %rdx
movq -0x10(%rdx), %rcx
movq %rcx, -0x78(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rcx, -0x80(%rbp)
movq -0x88(%rbp), %rax
movb $0x0, (%rax,%rcx)
movq %rdx, -0x10(%rdx)
leaq -0x68(%rbp), %rsi
movq $0x10, (%rsi)
leaq -0xc8(%rbp), %rdi
xorl %edx, %edx
callq 0x2420
movq %rax, -0xc8(%rbp)
movq -0x68(%rbp), %rcx
movq %rcx, -0xb8(%rbp)
movaps -0x180(%rbp), %xmm0
movups %xmm0, (%rax)
movq %rcx, -0xc0(%rbp)
movq -0xc8(%rbp), %rax
movb $0x0, (%rax,%rcx)
leaq -0x2d0(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0x2e0(%rbp), %rdi
callq 0x1372a
leaq -0x2b0(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0xc8(%rbp), %rsi
movq -0xc0(%rbp), %rdx
addq %rsi, %rdx
leaq -0x2c0(%rbp), %rdi
callq 0x1372a
leaq -0x238(%rbp), %rdi
leaq -0x220(%rbp), %rsi
leaq -0x2e0(%rbp), %rdx
leaq -0x2c0(%rbp), %rcx
callq 0xcec8
movq -0x2c0(%rbp), %rdi
cmpq %r15, %rdi
je 0x96a3
callq 0x2220
movq -0x2e0(%rbp), %rdi
cmpq %r14, %rdi
je 0x96b4
callq 0x2220
movq -0x238(%rbp), %rax
movq -0x230(%rbp), %rcx
subq %rax, %rcx
cmpq $0x20, %rcx
jne 0x9b76
leaq -0x1a6(%rbp), %r8
movq $0x0, -0x2(%r8)
movq -0x110(%rbp), %rdi
movq -0x130(%rbp), %rsi
movq (%rax), %rcx
leaq -0x190(%rbp), %rdx
callq 0x45f5
testb %al, %al
je 0x9b7f
movq -0x1a8(%rbp), %rax
bswapq %rax
movq %rax, -0x198(%rbp)
addq $0x20, %rax
movq %rax, -0xd0(%rbp)
bswapq %rax
leaq -0x19e(%rbp), %rcx
movq %rax, -0x2(%rcx)
leaq 0xe8fd(%rip), %rdi      # 0x18031
leaq 0xec5c(%rip), %rsi      # 0x18397
callq 0x22f0
movq %rax, %r14
leaq -0x190(%rbp), %r15
movl $0x10, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rax, %rcx
callq 0x23a0
movq %r14, %rdi
callq 0x2240
movq -0x110(%rbp), %rdi
movq -0x130(%rbp), %rdx
subq $0x8, %rsp
leaq -0x2a(%rbp), %rsi
leaq -0x180(%rbp), %r9
leaq -0x19e(%rbp), %rcx
movq %r15, %r8
pushq %rbx
pushq %r13
pushq %r12
callq 0x454e
addq $0x20, %rsp
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0xa8(%rbp), %rsi
movq $0x10, (%rsi)
leaq -0x68(%rbp), %rdi
xorl %edx, %edx
callq 0x2420
movq %rax, -0x68(%rbp)
leaq -0x98(%rbp), %rdx
movq -0x10(%rdx), %rcx
movq %rcx, -0x58(%rbp)
movaps -0x180(%rbp), %xmm0
movups %xmm0, (%rax)
movq %rcx, -0x60(%rbp)
movq -0x68(%rbp), %rax
movb $0x0, (%rax,%rcx)
movq %rdx, -0x10(%rdx)
leaq -0xf0(%rbp), %rsi
movq $0x10, (%rsi)
leaq -0xa8(%rbp), %rdi
xorl %edx, %edx
callq 0x2420
movq %rax, -0xa8(%rbp)
movq -0xf0(%rbp), %rcx
movq %rcx, -0x98(%rbp)
movaps -0x190(%rbp), %xmm0
movups %xmm0, (%rax)
movq %rcx, -0xa0(%rbp)
movq -0xa8(%rbp), %rax
movb $0x0, (%rax,%rcx)
leaq -0x290(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
addq %rsi, %rdx
leaq -0x2a0(%rbp), %rdi
callq 0x1372a
leaq -0x270(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x280(%rbp), %rdi
callq 0x1372a
leaq -0x48(%rbp), %rdi
leaq -0x220(%rbp), %rsi
leaq -0x2a0(%rbp), %rdx
leaq -0x280(%rbp), %rcx
callq 0xcec8
movq -0x280(%rbp), %rdi
cmpq %r15, %rdi
je 0x98b0
callq 0x2220
movq -0x2a0(%rbp), %rdi
cmpq %r14, %rdi
je 0x98c1
callq 0x2220
cmpb $0x1, -0x220(%rbp)
jne 0x99c5
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x99c5
leaq -0x248(%rbp), %r12
leaq -0x1c0(%rbp), %r13
leaq 0xe689(%rip), %r15      # 0x17f7a
xorl %r14d, %r14d
movq %r12, -0x258(%rbp)
leaq -0x258(%rbp), %rdi
leaq 0xe734(%rip), %rsi      # 0x1803d
leaq 0xe738(%rip), %rdx      # 0x18048
callq 0x137c4
movq -0x48(%rbp), %rax
movq %r14, %rcx
shlq $0x5, %rcx
movq %r13, -0x1d0(%rbp)
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq -0x1d0(%rbp), %rdi
callq 0x1372a
movq -0x258(%rbp), %rsi
leaq 0xf48b(%rip), %rdi      # 0x18dd8
xorl %eax, %eax
callq 0x2060
cmpq $0x0, -0x1c8(%rbp)
je 0x9981
xorl %ebx, %ebx
movq -0x1d0(%rbp), %rax
movsbl (%rax,%rbx), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x2060
incq %rbx
cmpq -0x1c8(%rbp), %rbx
jb 0x9960
movl $0xa, %edi
callq 0x2340
movq -0x1d0(%rbp), %rdi
cmpq %r13, %rdi
je 0x999c
callq 0x2220
movq -0x258(%rbp), %rdi
cmpq %r12, %rdi
je 0x99ad
callq 0x2220
incq %r14
movq -0x40(%rbp), %rax
subq -0x48(%rbp), %rax
sarq $0x5, %rax
cmpq %rax, %r14
jb 0x98f4
movq -0x40(%rbp), %rax
subq -0x48(%rbp), %rax
cmpq $0x80, %rax
jne 0x9c5a
leaq -0xe0(%rbp), %r14
movq %r14, -0x10(%r14)
leaq -0x150(%rbp), %rsi
movq $0x10, (%rsi)
leaq -0xf0(%rbp), %rdi
xorl %edx, %edx
leaq -0x120(%rbp), %r13
callq 0x2420
movq %rax, -0xf0(%rbp)
leaq -0x140(%rbp), %rbx
movq -0x10(%rbx), %rcx
movq %rcx, -0xe0(%rbp)
movaps -0x340(%rbp), %xmm0
movups %xmm0, (%rax)
movq %rcx, -0xe8(%rbp)
movq -0xf0(%rbp), %rax
movb $0x0, (%rax,%rcx)
movq %rbx, -0x10(%rbx)
leaq -0x360(%rbp), %rsi
movq $0x10, (%rsi)
leaq -0x150(%rbp), %rdi
xorl %edx, %edx
callq 0x2420
movq %rax, -0x150(%rbp)
leaq -0x350(%rbp), %r15
movq -0x10(%r15), %rcx
movq %rcx, -0x140(%rbp)
movaps -0x330(%rbp), %xmm0
movups %xmm0, (%rax)
movq %rcx, -0x148(%rbp)
movq -0x150(%rbp), %rax
xorl %edx, %edx
movb %dl, (%rax,%rcx)
movq %r15, -0x10(%r15)
movq -0x1b0(%rbp), %rax
movq %rax, (%r15)
movq $0x8, -0x8(%r15)
movb %dl, 0x8(%r15)
movq -0x48(%rbp), %r12
cmpq $0x8, 0x8(%r12)
jne 0x9b20
movq (%r12), %rax
movq -0x350(%rbp), %rcx
cmpq (%rax), %rcx
jne 0x9b20
movq -0x148(%rbp), %rdx
cmpq 0x28(%r12), %rdx
jne 0x9b20
testq %rdx, %rdx
je 0x9af0
movq 0x20(%r12), %rsi
movq -0x150(%rbp), %rdi
callq 0x2260
testl %eax, %eax
jne 0x9b20
movq -0xe8(%rbp), %rdx
cmpq 0x48(%r12), %rdx
jne 0x9b20
testq %rdx, %rdx
je 0x9cc0
movq 0x40(%r12), %rsi
movq -0xf0(%rbp), %rdi
callq 0x2260
testl %eax, %eax
je 0x9cc0
leaq 0xf384(%rip), %rdi      # 0x18eab
callq 0x23c0
movq -0x360(%rbp), %rdi
cmpq %r15, %rdi
leaq -0x100(%rbp), %r15
leaq -0x160(%rbp), %r12
je 0x9b4b
callq 0x2220
movq -0x150(%rbp), %rdi
cmpq %rbx, %rdi
je 0x9b5c
callq 0x2220
movq -0xf0(%rbp), %rdi
cmpq %r14, %rdi
je 0x9c84
callq 0x2220
jmp 0x9c84
leaq 0xf374(%rip), %rdi      # 0x18ef1
jmp 0x9b86
leaq 0xf2e8(%rip), %rdi      # 0x18e6e
callq 0x23c0
leaq -0x120(%rbp), %r13
leaq -0x100(%rbp), %r15
leaq -0x160(%rbp), %r12
leaq -0x238(%rbp), %rdi
callq 0xafd4
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0x9bc4
callq 0x2220
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x9bd9
callq 0x2220
movq -0x170(%rbp), %rdi
cmpq %r12, %rdi
je 0x9bea
callq 0x2220
movq -0x1f8(%rbp), %rdi
leaq -0x1e8(%rbp), %rax
cmpq %rax, %rdi
leaq -0x208(%rbp), %rbx
je 0x9c09
callq 0x2220
movq -0x218(%rbp), %rdi
cmpq %rbx, %rdi
je 0x9c1a
callq 0x2220
leaq -0x220(%rbp), %rdi
callq 0xb13c
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x9c37
callq 0x2220
movq -0x130(%rbp), %rdi
cmpq %r13, %rdi
je 0x9c48
callq 0x2220
addq $0x338, %rsp            # imm = 0x338
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe3e8(%rip), %rdi      # 0x18049
movq -0xd0(%rbp), %rsi
xorl %eax, %eax
callq 0x2060
leaq -0x120(%rbp), %r13
leaq -0x100(%rbp), %r15
leaq -0x160(%rbp), %r12
leaq -0x48(%rbp), %rdi
callq 0xafd4
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x9ca5
callq 0x2220
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x9ba0
callq 0x2220
jmp 0x9ba0
leaq 0xe3ab(%rip), %rdi      # 0x18072
movq -0xd0(%rbp), %rsi
xorl %eax, %eax
callq 0x2060
movq -0x198(%rbp), %rsi
addq $0x40, %rsi
leaq 0xe3b2(%rip), %rdi      # 0x18099
xorl %eax, %eax
callq 0x2060
jmp 0x9b2c
callq 0x3916
callq 0x397a
movq %rbx, %rdi
callq 0x39de
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
cmpq %r14, %rdi
je 0x9e1a
jmp 0x9e15
jmp 0x9e01
movq %rax, %rbx
movq -0x280(%rbp), %rdi
cmpq %r15, %rdi
je 0x9d3b
callq 0x2220
jmp 0x9d3b
movq %rax, %rbx
movq -0x2a0(%rbp), %rdi
cmpq %r14, %rdi
je 0x9e23
callq 0x2220
jmp 0x9e23
movq %rax, %rbx
jmp 0x9e23
movq %rax, %rbx
jmp 0x9e3b
movq %rax, %rbx
jmp 0x9e4d
movq %rax, %rbx
movq -0x2c0(%rbp), %rdi
cmpq %r15, %rdi
je 0x9d86
callq 0x2220
jmp 0x9d86
movq %rax, %rbx
movq -0x2e0(%rbp), %rdi
cmpq %r14, %rdi
je 0x9e59
callq 0x2220
jmp 0x9e59
movq %rax, %rbx
jmp 0x9e59
movq %rax, %rbx
jmp 0x9e71
jmp 0x9db4
jmp 0x9db4
movq %rax, %rbx
jmp 0x9e86
movq %rax, %rbx
movq -0x300(%rbp), %rdi
cmpq %r15, %rdi
je 0x9eaa
callq 0x2220
jmp 0x9eaa
movq %rax, %rbx
jmp 0x9eaa
movq %rax, %rbx
movq -0x320(%rbp), %rdi
cmpq %r12, %rdi
je 0x9ec2
jmp 0x9ebd
movq %rax, %rbx
jmp 0x9ec2
movq %rax, %rbx
jmp 0x9e1a
movq %rax, %rbx
movq -0x258(%rbp), %rdi
cmpq %r12, %rdi
je 0x9e1a
callq 0x2220
leaq -0x48(%rbp), %rdi
callq 0xafd4
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x9e3b
callq 0x2220
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x9e4d
callq 0x2220
leaq -0x238(%rbp), %rdi
callq 0xafd4
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0x9e71
callq 0x2220
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x9e86
callq 0x2220
movq -0x170(%rbp), %rdi
leaq -0x160(%rbp), %rax
cmpq %rax, %rdi
je 0x9e9e
callq 0x2220
leaq -0x220(%rbp), %rdi
callq 0xbc40
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x9ec2
callq 0x2220
movq -0x130(%rbp), %rdi
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x9eda
callq 0x2220
movq %rbx, %rdi
callq 0x2400