<s> from prob33 import group5_p , group5_g , mypow , secretToKeys , intToBytes <EOL> from random import randrange <EOL> from prob11 import generateAESKey <EOL> from prob10 import aes_cbc_enc , aes_cbc_dec <EOL> from prob9 import addPKCS7Padding <EOL> from prob13 import removePKCS7Padding <EOL> from prob15 import checkAndRemovePKCS7Padding <EOL> def message1 ( ) : <EOL> state = { } ; <EOL> state [ "<STR_LIT:p>" ] = group5_p ; <EOL> state [ "<STR_LIT:g>" ] = group5_g <EOL> return state ; <EOL> def message2 ( state ) : <EOL> return state ; <EOL> def message3 ( state ) : <EOL> state [ "<STR_LIT:a>" ] = randrange ( <NUM_LIT:2> , group5_p - <NUM_LIT:2> ) ; <EOL> state [ "<STR_LIT:A>" ] = mypow ( group5_g , state [ "<STR_LIT:a>" ] , group5_p ) ; <EOL> return state ; <EOL> def message4 ( state ) : <EOL> state [ "<STR_LIT:b>" ] = randrange ( <NUM_LIT:2> , state [ "<STR_LIT:p>" ] - <NUM_LIT:2> ) <EOL> state [ "<STR_LIT:B>" ] = mypow ( state [ "<STR_LIT:g>" ] , state [ "<STR_LIT:b>" ] , state [ "<STR_LIT:p>" ] ) <EOL> return state ; <EOL> def message5 ( state ) : <EOL> message = b"<STR_LIT>" ; <EOL> secret = mypow ( state [ "<STR_LIT:B>" ] , state [ "<STR_LIT:a>" ] , group5_p ) ; <EOL> state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] = secretToKeys ( intToBytes ( secret ) ) ; <EOL> state [ "<STR_LIT>" ] = generateAESKey ( ) ; <EOL> state [ "<STR_LIT>" ] = aes_cbc_enc ( addPKCS7Padding ( message , <NUM_LIT:16> ) , state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] ) ; <EOL> return state ; <EOL> def message6 ( state ) : <EOL> secret = mypow ( state [ "<STR_LIT:A>" ] , state [ "<STR_LIT:b>" ] , state [ "<STR_LIT:p>" ] ) ; <EOL> state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] = secretToKeys ( intToBytes ( secret ) ) ; <EOL> b_iv = generateAESKey ( ) ; <EOL> received_message = removePKCS7Padding ( aes_cbc_dec ( state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] ) ) ; <EOL> b_cipher = aes_cbc_enc ( addPKCS7Padding ( received_message , <NUM_LIT:16> ) , state [ "<STR_LIT>" ] , b_iv ) ; <EOL> state [ "<STR_LIT>" ] = b_cipher ; <EOL> state [ "<STR_LIT>" ] = b_iv ; <EOL> state [ "<STR_LIT>" ] = received_message ; <EOL> return state ; <EOL> def final ( state ) : <EOL> state [ "<STR_LIT>" ] = removePKCS7Padding ( aes_cbc_dec ( state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] ) ) ; <EOL> return state ; <EOL> def check_protocol ( state ) : <EOL> assert ( state [ "<STR_LIT>" ] == state [ "<STR_LIT>" ] ) ; <EOL> def message1_5_g1 ( state ) : <EOL> state [ "<STR_LIT:g>" ] = <NUM_LIT:1> ; <EOL> return state ; <EOL> def message3_5_g1 ( state ) : <EOL> state [ "<STR_LIT:A>" ] = <NUM_LIT:1> ; <EOL> return state ; <EOL> def check_protocol_g1 ( state ) : <EOL> m_secret = <NUM_LIT:1> ; <EOL> m_cipherkey , m_mackey = secretToKeys ( intToBytes ( m_secret ) ) ; <EOL> m_plain_a = removePKCS7Padding ( aes_cbc_dec ( state [ "<STR_LIT>" ] , m_cipherkey , state [ "<STR_LIT>" ] ) ) ; <EOL> m_plain_b = removePKCS7Padding ( aes_cbc_dec ( state [ "<STR_LIT>" ] , m_cipherkey , state [ "<STR_LIT>" ] ) ) ; <EOL> assert ( m_plain_a == state [ "<STR_LIT>" ] ) ; <EOL> assert ( m_plain_b == state [ "<STR_LIT>" ] ) ; <EOL> def run_g1 ( ) : <EOL> state = message1 ( ) ; <EOL> state = message1_5_g1 ( state ) ; <EOL> state = message2 ( state ) ; <EOL> state = message3 ( state ) ; <EOL> state = message3_5_g1 ( state ) ; <EOL> state = message4 ( state ) ; <EOL> state = message5 ( state ) ; <EOL> state = message6 ( state ) ; <EOL> state = final ( state ) ; <EOL> check_protocol ( state ) ; <EOL> check_protocol_g1 ( state ) ; <EOL> def message1_5_gp ( state ) : <EOL> state [ "<STR_LIT:g>" ] = state [ "<STR_LIT:p>" ] ; <EOL> return state ; <EOL> def message3_5_gp ( state ) : <EOL> state [ "<STR_LIT:A>" ] = state [ "<STR_LIT:p>" ] ; <EOL> return state ; <EOL> def check_protocol_gp ( state ) : <EOL> m_secret = <NUM_LIT:0> ; <EOL> m_cipherkey , m_mackey = secretToKeys ( intToBytes ( m_secret ) ) ; <EOL> m_plain_a = removePKCS7Padding ( aes_cbc_dec ( state [ "<STR_LIT>" ] , m_cipherkey , state [ "<STR_LIT>" ] ) ) ; <EOL> m_plain_b = removePKCS7Padding ( aes_cbc_dec ( state [ "<STR_LIT>" ] , m_cipherkey , state [ "<STR_LIT>" ] ) ) ; <EOL> assert ( m_plain_a == state [ "<STR_LIT>" ] ) ; <EOL> assert ( m_plain_b == state [ "<STR_LIT>" ] ) ; <EOL> def run_gp ( ) : <EOL> state = message1 ( ) ; <EOL> state = message1_5_gp ( state ) ; <EOL> state = message2 ( state ) ; <EOL> state = message3 ( state ) ; <EOL> state = message3_5_gp ( state ) ; <EOL> state = message4 ( state ) ; <EOL> state = message5 ( state ) ; <EOL> state = message6 ( state ) ; <EOL> state = final ( state ) ; <EOL> check_protocol ( state ) ; <EOL> check_protocol_gp ( state ) ; <EOL> def message1_5_gp1 ( state ) : <EOL> state [ "<STR_LIT:g>" ] = state [ "<STR_LIT:p>" ] - <NUM_LIT:1> ; <EOL> return state ; <EOL> def message3_5_gp1 ( state ) : <EOL> state [ "<STR_LIT:A>" ] = state [ "<STR_LIT:p>" ] - <NUM_LIT:1> ; <EOL> return state ; <EOL> def message5_5_gp1 ( state ) : <EOL> cipherkey_plus1 , mackey_plus1 = secretToKeys ( intToBytes ( <NUM_LIT:1> ) ) ; <EOL> cipherkey_minus1 , mackey_minus1 = secretToKeys ( intToBytes ( state [ "<STR_LIT:p>" ] - <NUM_LIT:1> ) ) ; <EOL> plain_plus1 = aes_cbc_dec ( state [ "<STR_LIT>" ] , cipherkey_plus1 , state [ "<STR_LIT>" ] ) <EOL> plain_minus1 = aes_cbc_dec ( state [ "<STR_LIT>" ] , cipherkey_minus1 , state [ "<STR_LIT>" ] ) <EOL> plain = None ; <EOL> try : <EOL> plain = checkAndRemovePKCS7Padding ( plain_plus1 ) <EOL> state [ "<STR_LIT>" ] = cipherkey_plus1 <EOL> except ValueError : <EOL> plain = checkAndRemovePKCS7Padding ( plain_minus1 ) <EOL> state [ "<STR_LIT>" ] = cipherkey_minus1 <EOL> state [ "<STR_LIT>" ] = plain ; <EOL> state [ "<STR_LIT>" ] , b_mackey = secretToKeys ( intToBytes ( state [ "<STR_LIT:B>" ] ) ) <EOL> state [ "<STR_LIT>" ] = aes_cbc_enc ( addPKCS7Padding ( plain , <NUM_LIT:16> ) , state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] ) ; <EOL> return state ; <EOL> def message6_5_gp1 ( state ) : <EOL> state [ "<STR_LIT>" ] = removePKCS7Padding ( aes_cbc_dec ( state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] ) ) ; <EOL> state [ "<STR_LIT>" ] = aes_cbc_enc ( addPKCS7Padding ( state [ "<STR_LIT>" ] , <NUM_LIT:16> ) , state [ "<STR_LIT>" ] , state [ "<STR_LIT>" ] ) ; <EOL> return state ; <EOL> def check_protocol_gp1 ( state ) : <EOL> assert ( state [ "<STR_LIT>" ] == state [ "<STR_LIT>" ] ) ; <EOL> assert ( state [ "<STR_LIT>" ] == state [ "<STR_LIT>" ] ) ; <EOL> def run_gp1 ( ) : <EOL> state = message1 ( ) ; <EOL> state = message1_5_gp1 ( state ) ; <EOL> state = message2 ( state ) ; <EOL> state = message3 ( state ) ; <EOL> state = message3_5_gp1 ( state ) ; <EOL> state = message4 ( state ) ; <EOL> state = message5 ( state ) ; <EOL> state = message5_5_gp1 ( state ) ; <EOL> state = message6 ( state ) ; <EOL> state = message6_5_gp1 ( state ) ; <EOL> state = final ( state ) ; <EOL> check_protocol ( state ) ; <EOL> check_protocol_gp1 ( state ) ; <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_g1 ( ) ; <EOL> run_gp ( ) ; <EOL> run_gp1 ( ) ; <EOL> print ( "<STR_LIT>" ) ; </s>
