clear

reset

00 a4 04 00 00
if sw != 9000
    
?
    
pause
endif
set resp
isd = gettlv( $resp, 6f 84 )

00 a4 04 00 ( $isd )
if sw != 9000
    
?
    
pause
endif
set resp


call run_jcop_extauth( 00 )

end

run_jcop_extauth:
    
maclength                           = 8

    
auth_level                          = getpara

    
static_key                          = 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f

    
host_random                         = random( 8 )

    
80 50 00 00 ( $ host_random )
    
if sw != 9000
        
? "initial update error"
        
pause
    
endif
    
set resp
    
Key_diversification_data            = mid( $ resp, 0, 10 )      //Key diversification data
    Key_information                     = mid( $ resp, 10, 2 )              //Key information
    Sequence_counter                    = mid( $ resp, 12, 2 )              //Sequence counter
    Card_challenge                      = mid( $ resp, 14, 6 )              //Card challenge
    Card_cryptogram                     = mid( $ resp, 20, 8 )      //Card cryptogram


    
? "S-ENC = 2 byte constant + 2 byte sequence counter + 12 byte 00"
    
SENC_P                              = 01 82 $ Sequence_counter 00000000 00000000 00000000

    
? "C-MAC = 2 byte constant + 2 byte sequence counter + 12 byte 00"
    
CMAC_P                              = 01 01 $ Sequence_counter 00000000 00000000 00000000

    
? "C-DEK   = 2 byte constant + 2 byte sequence counter + 12 byte 00"
    
CDEK_P                              = 01 81 $ Sequence_counter 00000000 00000000 00000000

    
SENC_SESSION                        = 3des_encode_cbc( 0000000000000000, $ SENC_P, $ static_key )

    
CMAC_SESSION                        = 3des_encode_cbc( 0000000000000000, $ CMAC_P, $ static_key )

    
SDEK_SESSION                        = 3des_encode_cbc( 0000000000000000, $ CDEK_P, $ static_key )


    
? "Host Authenticate Cryptogram = sequence counter(2Byte) + card challenge(6) + host challenge(8) +80000000 00000000"
    
host_authenticate_cryptogram        = $ Sequence_counter   $ Card_challenge   $ host_random    80000000 00000000

    
host_authenticate_cryptogram_c      = 3des_encode_cbc( 00000000, $ host_authenticate_cryptogram, $ SENC_SESSION )

    
host_authenticate_cryptogram_MAC    = mid( $ host_authenticate_cryptogram_c, 16, 8 )

    
external_auth                       = 84 82 00 $ auth_level < $ host_authenticate_cryptogram_MAC >
    
pack_external_auth                  = fixed80( $ external_auth )

    
external_auth_mac                   = des_3des_mac( 0000000000000000, $ pack_external_auth, $ CMAC_SESSION )

    
$ external_auth $ external_auth_mac
    
if sw != 9000
        
? "external error"
        
pause
    
endif

return