private byte[] ProcessSMPMessage3(byte[] smp_byte_array, ref OTR.Interface.OTR_SMP_EVENT smp_event_type_1, ref OTR.Interface.OTR_SMP_EVENT smp_event_type_2, ref string message) { if (smp_byte_array == null || smp_byte_array.Length < 1) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: The SMP byte array cannot be null/empty"; ResetData(); return(null); } if (_smp_state != OTR_SMP_STATE.EXPECT_3) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Illigal state"; ResetData(); return(null); } int _start_index = 0; uint _mpi_count = 0; byte[] _mpis_buffer = null; Utility.DecodeTLVSMPMessage(smp_byte_array, _start_index, ref _mpi_count, ref _mpis_buffer); if (_mpi_count != MESSAGE_3_MPI_COUNT) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3:The MPI count must be " + MESSAGE_3_MPI_COUNT.ToString(); ResetData(); return(null); } byte[] _p_a_mpi = null; byte[] _q_a_mpi = null; byte[] _c_p_mpi = null; byte[] _d_5_mpi = null; byte[] _d_6_mpi = null; byte[] _r_a_mpi = null; byte[] _c_r_mpi = null; byte[] _d_7_mpi = null; _start_index = 0; _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _p_a_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _q_a_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_p_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_5_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_6_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _r_a_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_r_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_7_mpi); _smp_third_message_data = new BigInteger[_mpi_count]; Utility.DecodeMpiFromBytes(_p_a_mpi, 0, ref _smp_third_message_data[0]); Utility.DecodeMpiFromBytes(_q_a_mpi, 0, ref _smp_third_message_data[1]); Utility.DecodeMpiFromBytes(_c_p_mpi, 0, ref _smp_third_message_data[2]); Utility.DecodeMpiFromBytes(_d_5_mpi, 0, ref _smp_third_message_data[3]); Utility.DecodeMpiFromBytes(_d_6_mpi, 0, ref _smp_third_message_data[4]); Utility.DecodeMpiFromBytes(_r_a_mpi, 0, ref _smp_third_message_data[5]); Utility.DecodeMpiFromBytes(_c_r_mpi, 0, ref _smp_third_message_data[6]); Utility.DecodeMpiFromBytes(_d_7_mpi, 0, ref _smp_third_message_data[7]); if (IsValidValue(_smp_third_message_data[0]) == false) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Pa is not valid"; ResetData(); return(null); } if (IsValidValue(_smp_third_message_data[1]) == false) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Qa is not valid"; ResetData(); return(null); } if (IsValidValue(_smp_third_message_data[5]) == false) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Ra is not valid"; ResetData(); return(null); } if (VerifyZKProof(6, _smp_third_message_data[0], _smp_third_message_data[1], _smp_third_message_data[2], _smp_third_message_data[3], _smp_third_message_data[4]) == false) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Zero-Knowledge Proof (Prefix 6) failed"; ResetData(); return(null); } BigInteger _inverse = _session_object.Q.ModInverse(PRIME_MODULO); _session_object.Qa_Qb = _smp_third_message_data[1].Multiply(_inverse).Mod(PRIME_MODULO); _inverse = _session_object.P.ModInverse(PRIME_MODULO); _session_object.Pa_Pb = _smp_third_message_data[0].Multiply(_inverse).Mod(PRIME_MODULO); if (VerifyZKProofA(7, _smp_third_message_data[5], _smp_third_message_data[6], _smp_third_message_data[7]) == false) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Zero-Knowledge Proof (Prefix 7) failed"; ResetData(); return(null); } BigInteger _r_ab = _smp_third_message_data[5].ModPow(_session_object.EXP_2, PRIME_MODULO); _smp_state = OTR_SMP_STATE.EXPECT_1; if (_r_ab.Equals(_session_object.Pa_Pb) == true) { smp_event_type_2 = OTR.Interface.OTR_SMP_EVENT.SUCCEEDED; // Console.WriteLine("SMP completed succesfully \n"); message = "ProcessSMPMessage3: SMP completed succesfully"; ResetData(); } else { smp_event_type_2 = OTR.Interface.OTR_SMP_EVENT.FAILED; // Console.WriteLine("SMP Man in the middle suspected \n"); message = "ProcessSMPMessage3: SMP Man in the middle suspected"; ResetData(); } smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.SEND; return(FormatSMPMessage4()); }
private byte[] ProcessSMPMessage1(byte[] smp_byte_array, ref OTR.Interface.OTR_SMP_EVENT smp_event_type, ref string message) { if (smp_byte_array == null || smp_byte_array.Length < 1) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: The SMP byte array cannot be null/empty"; ResetData(); return(null); } if (_smp_state != OTR_SMP_STATE.EXPECT_1) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: Illigal state"; ResetData(); return(null); } int _start_index = 0; uint _mpi_count = 0; byte[] _mpis_buffer = null; byte[] _g_2_mpi = null; byte[] _c_2_mpi = null; byte[] _d_2_mpi = null; byte[] _g_3_mpi = null; byte[] _c_3_mpi = null; byte[] _d_3_mpi = null; Utility.DecodeTLVSMPMessage(smp_byte_array, _start_index, ref _mpi_count, ref _mpis_buffer); if (_mpi_count != MESSAGE_1_MPI_COUNT) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: The MPI count must be " + MESSAGE_1_MPI_COUNT.ToString(); ResetData(); return(null); } _start_index = 0; _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _g_2_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_2_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_2_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _g_3_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_3_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_3_mpi); _smp_first_message_data = new BigInteger[_mpi_count]; Utility.DecodeMpiFromBytes(_g_2_mpi, 0, ref _smp_first_message_data[0]); Utility.DecodeMpiFromBytes(_c_2_mpi, 0, ref _smp_first_message_data[1]); Utility.DecodeMpiFromBytes(_d_2_mpi, 0, ref _smp_first_message_data[2]); Utility.DecodeMpiFromBytes(_g_3_mpi, 0, ref _smp_first_message_data[3]); Utility.DecodeMpiFromBytes(_c_3_mpi, 0, ref _smp_first_message_data[4]); Utility.DecodeMpiFromBytes(_d_3_mpi, 0, ref _smp_first_message_data[5]); if (IsValidValue(_smp_first_message_data[0]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: G2a is not valid"; ResetData(); return(null); } if (IsValidValue(_smp_first_message_data[3]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: G3a is not valid"; ResetData(); return(null); } if (VerifyZKProof(1, _smp_first_message_data[0], _smp_first_message_data[1], _smp_first_message_data[2]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: Zero-Knowledge Proof (Prefix 1) failed"; ResetData(); return(null); } if (VerifyZKProof(2, _smp_first_message_data[3], _smp_first_message_data[4], _smp_first_message_data[5]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: Zero-Knowledge Proof (Prefix 2) failed"; ResetData(); return(null); } _smp_state = OTR_SMP_STATE.EXPECT_3; smp_event_type = OTR.Interface.OTR_SMP_EVENT.SEND; return(FormatSMPMessage2()); }
private byte[] ProcessSMPMessage2(byte[] smp_byte_array, ref OTR.Interface.OTR_SMP_EVENT smp_event_type, ref string message) { if (_smp_state != OTR_SMP_STATE.EXPECT_2) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: The SMP byte array cannot be null/empty"; ResetData(); return(null); } int _start_index = 0; uint _mpi_count = 0; byte[] _mpis_buffer = null; Utility.DecodeTLVSMPMessage(smp_byte_array, _start_index, ref _mpi_count, ref _mpis_buffer); if (_mpi_count != MESSAGE_2_MPI_COUNT) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: The MPI count must be " + MESSAGE_2_MPI_COUNT.ToString(); ResetData(); return(null); } byte[] _g_2_b_mpi = null; byte[] _c_2_mpi = null; byte[] _d_2_mpi = null; byte[] _g_3_b_mpi = null; byte[] _c_3_mpi = null; byte[] _d_3_mpi = null; byte[] _p_b_mpi = null; byte[] _q_b_mpi = null; byte[] _c_p_mpi = null; byte[] _d_5_mpi = null; byte[] _d_6_mpi = null; _start_index = 0; _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _g_2_b_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_2_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_2_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _g_3_b_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_3_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_3_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _p_b_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _q_b_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_p_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_5_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_6_mpi); _smp_second_message_data = new BigInteger[_mpi_count]; Utility.DecodeMpiFromBytes(_g_2_b_mpi, 0, ref _smp_second_message_data[0]); Utility.DecodeMpiFromBytes(_c_2_mpi, 0, ref _smp_second_message_data[1]); Utility.DecodeMpiFromBytes(_d_2_mpi, 0, ref _smp_second_message_data[2]); Utility.DecodeMpiFromBytes(_g_3_b_mpi, 0, ref _smp_second_message_data[3]); Utility.DecodeMpiFromBytes(_c_3_mpi, 0, ref _smp_second_message_data[4]); Utility.DecodeMpiFromBytes(_d_3_mpi, 0, ref _smp_second_message_data[5]); Utility.DecodeMpiFromBytes(_p_b_mpi, 0, ref _smp_second_message_data[6]); Utility.DecodeMpiFromBytes(_q_b_mpi, 0, ref _smp_second_message_data[7]); Utility.DecodeMpiFromBytes(_c_p_mpi, 0, ref _smp_second_message_data[8]); Utility.DecodeMpiFromBytes(_d_5_mpi, 0, ref _smp_second_message_data[9]); Utility.DecodeMpiFromBytes(_d_6_mpi, 0, ref _smp_second_message_data[10]); if (IsValidValue(_smp_second_message_data[0]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: G2b is not valid"; ResetData(); return(null); } if (IsValidValue(_smp_second_message_data[3]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: G3b is not valid"; ResetData(); return(null); } if (IsValidValue(_smp_second_message_data[6]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: P is not valid"; ResetData(); return(null); } if (IsValidValue(_smp_second_message_data[7]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: Q is not valid"; ResetData(); return(null); } _session_object.G_2 = _smp_second_message_data[0].ModPow(_session_object.EXP_1, PRIME_MODULO); _session_object.G_3 = _smp_second_message_data[3].ModPow(_session_object.EXP_2, PRIME_MODULO); if (VerifyZKProof(3, _smp_second_message_data[0], _smp_second_message_data[1], _smp_second_message_data[2]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: Zero-Knowledge Proof (Prefix 3) failed"; ResetData(); return(null); } if (VerifyZKProof(4, _smp_second_message_data[3], _smp_second_message_data[4], _smp_second_message_data[5]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: Zero-Knowledge Proof (Prefix 4) failed"; ResetData(); return(null); } if (VerifyZKProof(5, _smp_second_message_data[6], _smp_second_message_data[7], _smp_second_message_data[8], _smp_second_message_data[9], _smp_second_message_data[10]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: Zero-Knowledge Proof (Prefix 5) failed"; ResetData(); return(null); } _smp_state = OTR_SMP_STATE.EXPECT_4; smp_event_type = OTR.Interface.OTR_SMP_EVENT.SEND; return(FormatSMPMessage3()); }
private void ResetData() { _smp_state = OTR_SMP_STATE.EXPECT_1; _is_started = false; }
public byte[] FormatSMPMessage1() { SetFirstMessageData(); if (_smp_first_message_data == null || _smp_first_message_data.Length < MESSAGE_1_MPI_COUNT) { throw new InvalidDataException("FormatSMPMessage1:The SMP first message data cannot be null or have elements less than " + MESSAGE_1_MPI_COUNT.ToString()); } byte[] _temp_mpi_buff = null; byte[] _message_buff = null; byte[] _g_2_mpi = null; byte[] _c_2_mpi = null; byte[] _d_2_mpi = null; byte[] _g_3_mpi = null; byte[] _c_3_mpi = null; byte[] _d_3_mpi = null; int _buff_length = 0; int _next_index = 0; Utility.EncodeMpiBytes(_smp_first_message_data[0], ref _g_2_mpi); Utility.EncodeMpiBytes(_smp_first_message_data[1], ref _c_2_mpi); Utility.EncodeMpiBytes(_smp_first_message_data[2], ref _d_2_mpi); Utility.EncodeMpiBytes(_smp_first_message_data[3], ref _g_3_mpi); Utility.EncodeMpiBytes(_smp_first_message_data[4], ref _c_3_mpi); Utility.EncodeMpiBytes(_smp_first_message_data[5], ref _d_3_mpi); _buff_length = _g_2_mpi.Length + _c_2_mpi.Length + _d_2_mpi.Length; _buff_length += _g_3_mpi.Length + _c_3_mpi.Length + _d_3_mpi.Length; _temp_mpi_buff = new byte[_buff_length]; Buffer.BlockCopy(_g_2_mpi, 0, _temp_mpi_buff, _next_index, _g_2_mpi.Length); _next_index += _g_2_mpi.Length; Buffer.BlockCopy(_c_2_mpi, 0, _temp_mpi_buff, _next_index, _c_2_mpi.Length); _next_index += _c_2_mpi.Length; Buffer.BlockCopy(_d_2_mpi, 0, _temp_mpi_buff, _next_index, _d_2_mpi.Length); _next_index += _d_2_mpi.Length; Buffer.BlockCopy(_g_3_mpi, 0, _temp_mpi_buff, _next_index, _g_3_mpi.Length); _next_index += _g_3_mpi.Length; Buffer.BlockCopy(_c_3_mpi, 0, _temp_mpi_buff, _next_index, _c_3_mpi.Length); _next_index += _c_3_mpi.Length; Buffer.BlockCopy(_d_3_mpi, 0, _temp_mpi_buff, _next_index, _d_3_mpi.Length); _next_index += _d_3_mpi.Length; Utility.EncodeTLVSMPMessage(OTR_TLV_TYPE.SMP_MESSAGE_1, MESSAGE_1_MPI_COUNT, _temp_mpi_buff, ref _message_buff); _smp_state = OTR_SMP_STATE.EXPECT_2; return(_message_buff); }
private void ResetData() { _smp_state = OTR_SMP_STATE.EXPECT_1; _is_started = false; }
private byte[] ProcessSMPMessage4(byte[] smp_byte_array, ref OTR.Interface.OTR_SMP_EVENT smp_event_type, ref string message) { if (smp_byte_array == null || smp_byte_array.Length < 1) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage4: The SMP byte array cannot be null/empty"; ResetData(); return(null); } if (_smp_state != OTR_SMP_STATE.EXPECT_4) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage4: Illigal state"; ResetData(); return(null); } int _start_index = 0; uint _mpi_count = 0; byte[] _mpis_buffer = null; Utility.DecodeTLVSMPMessage(smp_byte_array, _start_index, ref _mpi_count, ref _mpis_buffer); if (_mpi_count != MESSAGE_4_MPI_COUNT) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage4: The MPI count must be " + MESSAGE_4_MPI_COUNT.ToString(); ResetData(); return(null); } byte[] _r_b_mpi = null; byte[] _c_mpi = null; byte[] _d_mpi = null; BigInteger _r_b = null; BigInteger _c = null; BigInteger _d = null; _start_index = 0; _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _r_b_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_mpi); Utility.DecodeMpiFromBytes(_r_b_mpi, 0, ref _r_b); Utility.DecodeMpiFromBytes(_c_mpi, 0, ref _c); Utility.DecodeMpiFromBytes(_d_mpi, 0, ref _d); if (IsValidValue(_r_b) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage4: Rb is not valid"; ResetData(); return(null); } if (VerifyZKProofA(8, _r_b, _c, _d) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage4: Zero-Knowledge Proof (Prefix 8) failed"; ResetData(); return(null); } _smp_state = OTR_SMP_STATE.EXPECT_1; BigInteger _r_ab = _r_b.ModPow(_session_object.EXP_2, PRIME_MODULO); if (_r_ab.Equals(_session_object.Pa_Pb) == true) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.SUCCEEDED; // Console.WriteLine("SMP completed succesfully \n"); message = "ProcessSMPMessage4: SMP completed succesfully"; ResetData(); } else { smp_event_type = OTR.Interface.OTR_SMP_EVENT.FAILED; // Console.WriteLine("SMP Man in the middle suspected \n"); message = "ProcessSMPMessage4: SMP Man in the middle suspected"; ResetData(); } return(null); }
private byte[] ProcessSMPMessage4(byte[] smp_byte_array, ref OTR.Interface.OTR_SMP_EVENT smp_event_type, ref string message) { if (smp_byte_array == null || smp_byte_array.Length < 1) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage4: The SMP byte array cannot be null/empty"; ResetData(); return null; } if (_smp_state != OTR_SMP_STATE.EXPECT_4) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage4: Illigal state"; ResetData(); return null; } int _start_index = 0; uint _mpi_count = 0; byte[] _mpis_buffer = null; Utility.DecodeTLVSMPMessage(smp_byte_array, _start_index, ref _mpi_count, ref _mpis_buffer); if (_mpi_count != MESSAGE_4_MPI_COUNT) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage4: The MPI count must be " + MESSAGE_4_MPI_COUNT.ToString(); ResetData(); return null; } byte[] _r_b_mpi = null; byte[] _c_mpi = null; byte[] _d_mpi = null; BigInteger _r_b = null; BigInteger _c = null; BigInteger _d = null; _start_index = 0; _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _r_b_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_mpi); Utility.DecodeMpiFromBytes(_r_b_mpi, 0, ref _r_b); Utility.DecodeMpiFromBytes(_c_mpi, 0, ref _c); Utility.DecodeMpiFromBytes(_d_mpi, 0, ref _d); if (IsValidValue(_r_b) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage4: Rb is not valid"; ResetData(); return null; } if (VerifyZKProofA(8, _r_b,_c, _d) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage4: Zero-Knowledge Proof (Prefix 8) failed"; ResetData(); return null; } _smp_state = OTR_SMP_STATE.EXPECT_1; BigInteger _r_ab = _r_b.ModPow(_session_object.EXP_2, PRIME_MODULO); if (_r_ab.Equals(_session_object.Pa_Pb) == true) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.SUCCEEDED; // Console.WriteLine("SMP completed succesfully \n"); message = "ProcessSMPMessage4: SMP completed succesfully"; ResetData(); } else { smp_event_type = OTR.Interface.OTR_SMP_EVENT.FAILED; // Console.WriteLine("SMP Man in the middle suspected \n"); message = "ProcessSMPMessage4: SMP Man in the middle suspected"; ResetData(); } return null; }
public byte[] FormatSMPMessage1() { SetFirstMessageData(); if (_smp_first_message_data == null || _smp_first_message_data.Length < MESSAGE_1_MPI_COUNT) throw new InvalidDataException("FormatSMPMessage1:The SMP first message data cannot be null or have elements less than " + MESSAGE_1_MPI_COUNT.ToString()); byte[] _temp_mpi_buff = null; byte[] _message_buff = null; byte[] _g_2_mpi = null; byte[] _c_2_mpi = null; byte[] _d_2_mpi = null; byte[] _g_3_mpi = null; byte[] _c_3_mpi = null; byte[] _d_3_mpi = null; int _buff_length = 0; int _next_index = 0; Utility.EncodeMpiBytes(_smp_first_message_data[0], ref _g_2_mpi); Utility.EncodeMpiBytes(_smp_first_message_data[1], ref _c_2_mpi); Utility.EncodeMpiBytes(_smp_first_message_data[2], ref _d_2_mpi); Utility.EncodeMpiBytes(_smp_first_message_data[3], ref _g_3_mpi); Utility.EncodeMpiBytes(_smp_first_message_data[4], ref _c_3_mpi); Utility.EncodeMpiBytes(_smp_first_message_data[5], ref _d_3_mpi); _buff_length = _g_2_mpi.Length + _c_2_mpi.Length + _d_2_mpi.Length; _buff_length += _g_3_mpi.Length + _c_3_mpi.Length + _d_3_mpi.Length; _temp_mpi_buff = new byte[_buff_length]; Buffer.BlockCopy(_g_2_mpi, 0, _temp_mpi_buff, _next_index, _g_2_mpi.Length); _next_index += _g_2_mpi.Length; Buffer.BlockCopy(_c_2_mpi, 0, _temp_mpi_buff, _next_index, _c_2_mpi.Length); _next_index += _c_2_mpi.Length; Buffer.BlockCopy(_d_2_mpi, 0, _temp_mpi_buff, _next_index, _d_2_mpi.Length); _next_index += _d_2_mpi.Length; Buffer.BlockCopy(_g_3_mpi, 0, _temp_mpi_buff, _next_index, _g_3_mpi.Length); _next_index += _g_3_mpi.Length; Buffer.BlockCopy(_c_3_mpi, 0, _temp_mpi_buff, _next_index, _c_3_mpi.Length); _next_index += _c_3_mpi.Length; Buffer.BlockCopy(_d_3_mpi, 0, _temp_mpi_buff, _next_index, _d_3_mpi.Length); _next_index += _d_3_mpi.Length; Utility.EncodeTLVSMPMessage(OTR_TLV_TYPE.SMP_MESSAGE_1, MESSAGE_1_MPI_COUNT, _temp_mpi_buff, ref _message_buff); _smp_state = OTR_SMP_STATE.EXPECT_2; return _message_buff; }
private byte[] ProcessSMPMessage3(byte[] smp_byte_array, ref OTR.Interface.OTR_SMP_EVENT smp_event_type_1, ref OTR.Interface.OTR_SMP_EVENT smp_event_type_2, ref string message) { if (smp_byte_array == null || smp_byte_array.Length < 1) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: The SMP byte array cannot be null/empty"; ResetData(); return null; } if (_smp_state != OTR_SMP_STATE.EXPECT_3) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Illigal state"; ResetData(); return null; } int _start_index = 0; uint _mpi_count = 0; byte[] _mpis_buffer = null; Utility.DecodeTLVSMPMessage(smp_byte_array, _start_index, ref _mpi_count, ref _mpis_buffer); if (_mpi_count != MESSAGE_3_MPI_COUNT) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3:The MPI count must be " + MESSAGE_3_MPI_COUNT.ToString(); ResetData(); return null; } byte[] _p_a_mpi = null; byte[] _q_a_mpi = null; byte[] _c_p_mpi = null; byte[] _d_5_mpi = null; byte[] _d_6_mpi = null; byte[] _r_a_mpi = null; byte[] _c_r_mpi = null; byte[] _d_7_mpi = null; _start_index = 0; _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _p_a_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _q_a_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_p_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_5_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_6_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _r_a_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_r_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_7_mpi); _smp_third_message_data = new BigInteger[_mpi_count]; Utility.DecodeMpiFromBytes(_p_a_mpi, 0, ref _smp_third_message_data[0]); Utility.DecodeMpiFromBytes(_q_a_mpi, 0, ref _smp_third_message_data[1]); Utility.DecodeMpiFromBytes(_c_p_mpi, 0, ref _smp_third_message_data[2]); Utility.DecodeMpiFromBytes(_d_5_mpi, 0, ref _smp_third_message_data[3]); Utility.DecodeMpiFromBytes(_d_6_mpi, 0, ref _smp_third_message_data[4]); Utility.DecodeMpiFromBytes(_r_a_mpi, 0, ref _smp_third_message_data[5]); Utility.DecodeMpiFromBytes(_c_r_mpi, 0, ref _smp_third_message_data[6]); Utility.DecodeMpiFromBytes(_d_7_mpi, 0, ref _smp_third_message_data[7]); if (IsValidValue(_smp_third_message_data[0]) == false) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Pa is not valid"; ResetData(); return null; } if (IsValidValue(_smp_third_message_data[1]) == false) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Qa is not valid"; ResetData(); return null; } if (IsValidValue(_smp_third_message_data[5]) == false) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Ra is not valid"; ResetData(); return null; } if (VerifyZKProof(6, _smp_third_message_data[0], _smp_third_message_data[1], _smp_third_message_data[2], _smp_third_message_data[3], _smp_third_message_data[4]) == false) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Zero-Knowledge Proof (Prefix 6) failed"; ResetData(); return null; } BigInteger _inverse = _session_object.Q.ModInverse(PRIME_MODULO); _session_object.Qa_Qb = _smp_third_message_data[1].Multiply(_inverse).Mod(PRIME_MODULO); _inverse = _session_object.P.ModInverse(PRIME_MODULO); _session_object.Pa_Pb = _smp_third_message_data[0].Multiply(_inverse).Mod(PRIME_MODULO); if (VerifyZKProofA(7,_smp_third_message_data[5], _smp_third_message_data[6], _smp_third_message_data[7]) == false) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: Zero-Knowledge Proof (Prefix 7) failed"; ResetData(); return null; } BigInteger _r_ab = _smp_third_message_data[5].ModPow(_session_object.EXP_2, PRIME_MODULO); _smp_state = OTR_SMP_STATE.EXPECT_1; if (_r_ab.Equals(_session_object.Pa_Pb) == true) { smp_event_type_2 = OTR.Interface.OTR_SMP_EVENT.SUCCEEDED; // Console.WriteLine("SMP completed succesfully \n"); message = "ProcessSMPMessage3: SMP completed succesfully"; ResetData(); } else { smp_event_type_2 = OTR.Interface.OTR_SMP_EVENT.FAILED; // Console.WriteLine("SMP Man in the middle suspected \n"); message = "ProcessSMPMessage3: SMP Man in the middle suspected"; ResetData(); } smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.SEND; return FormatSMPMessage4(); }
private byte[] ProcessSMPMessage2(byte[] smp_byte_array, ref OTR.Interface.OTR_SMP_EVENT smp_event_type, ref string message) { if (_smp_state != OTR_SMP_STATE.EXPECT_2) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: The SMP byte array cannot be null/empty"; ResetData(); return null; } int _start_index = 0; uint _mpi_count = 0; byte[] _mpis_buffer = null; Utility.DecodeTLVSMPMessage(smp_byte_array, _start_index, ref _mpi_count, ref _mpis_buffer); if (_mpi_count != MESSAGE_2_MPI_COUNT) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: The MPI count must be " + MESSAGE_2_MPI_COUNT.ToString(); ResetData(); return null; } byte[] _g_2_b_mpi = null; byte[] _c_2_mpi = null; byte[] _d_2_mpi = null; byte[] _g_3_b_mpi = null; byte[] _c_3_mpi = null; byte[] _d_3_mpi = null; byte[] _p_b_mpi = null; byte[] _q_b_mpi = null; byte[] _c_p_mpi = null; byte[] _d_5_mpi = null; byte[] _d_6_mpi = null; _start_index = 0; _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _g_2_b_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_2_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_2_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _g_3_b_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_3_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_3_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _p_b_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _q_b_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_p_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_5_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_6_mpi); _smp_second_message_data = new BigInteger[_mpi_count]; Utility.DecodeMpiFromBytes(_g_2_b_mpi, 0, ref _smp_second_message_data[0]); Utility.DecodeMpiFromBytes(_c_2_mpi, 0, ref _smp_second_message_data[1]); Utility.DecodeMpiFromBytes(_d_2_mpi, 0, ref _smp_second_message_data[2]); Utility.DecodeMpiFromBytes(_g_3_b_mpi, 0, ref _smp_second_message_data[3]); Utility.DecodeMpiFromBytes(_c_3_mpi, 0, ref _smp_second_message_data[4]); Utility.DecodeMpiFromBytes(_d_3_mpi, 0, ref _smp_second_message_data[5]); Utility.DecodeMpiFromBytes(_p_b_mpi, 0, ref _smp_second_message_data[6]); Utility.DecodeMpiFromBytes(_q_b_mpi, 0, ref _smp_second_message_data[7]); Utility.DecodeMpiFromBytes(_c_p_mpi, 0, ref _smp_second_message_data[8]); Utility.DecodeMpiFromBytes(_d_5_mpi, 0, ref _smp_second_message_data[9]); Utility.DecodeMpiFromBytes(_d_6_mpi, 0, ref _smp_second_message_data[10]); if (IsValidValue(_smp_second_message_data[0]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: G2b is not valid"; ResetData(); return null; } if (IsValidValue(_smp_second_message_data[3]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: G3b is not valid"; ResetData(); return null; } if (IsValidValue(_smp_second_message_data[6]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: P is not valid"; ResetData(); return null; } if (IsValidValue(_smp_second_message_data[7]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: Q is not valid"; ResetData(); return null; } _session_object.G_2 = _smp_second_message_data[0].ModPow(_session_object.EXP_1, PRIME_MODULO); _session_object.G_3 = _smp_second_message_data[3].ModPow(_session_object.EXP_2, PRIME_MODULO); if (VerifyZKProof(3, _smp_second_message_data[0], _smp_second_message_data[1], _smp_second_message_data[2]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: Zero-Knowledge Proof (Prefix 3) failed"; ResetData(); return null; } if (VerifyZKProof(4, _smp_second_message_data[3], _smp_second_message_data[4], _smp_second_message_data[5]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: Zero-Knowledge Proof (Prefix 4) failed"; ResetData(); return null; } if (VerifyZKProof(5, _smp_second_message_data[6], _smp_second_message_data[7], _smp_second_message_data[8], _smp_second_message_data[9], _smp_second_message_data[10]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage2: Zero-Knowledge Proof (Prefix 5) failed"; ResetData(); return null; } _smp_state = OTR_SMP_STATE.EXPECT_4; smp_event_type = OTR.Interface.OTR_SMP_EVENT.SEND; return FormatSMPMessage3(); }
private byte[] ProcessSMPMessage1(byte[] smp_byte_array, ref OTR.Interface.OTR_SMP_EVENT smp_event_type, ref string message) { if (smp_byte_array == null || smp_byte_array.Length < 1) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: The SMP byte array cannot be null/empty"; ResetData(); return null; } if (_smp_state != OTR_SMP_STATE.EXPECT_1) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: Illigal state"; ResetData(); return null; } int _start_index = 0; uint _mpi_count = 0; byte[] _mpis_buffer = null; byte[] _g_2_mpi = null; byte[] _c_2_mpi = null; byte[] _d_2_mpi = null; byte[] _g_3_mpi = null; byte[] _c_3_mpi = null; byte[] _d_3_mpi = null; Utility.DecodeTLVSMPMessage(smp_byte_array, _start_index, ref _mpi_count, ref _mpis_buffer); if (_mpi_count != MESSAGE_1_MPI_COUNT) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: The MPI count must be " + MESSAGE_1_MPI_COUNT.ToString(); ResetData(); return null; } _start_index = 0; _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _g_2_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_2_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_2_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _g_3_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _c_3_mpi); _start_index = Utility.DecoupleMpiFromBytes(_mpis_buffer, _start_index, ref _d_3_mpi); _smp_first_message_data = new BigInteger[_mpi_count]; Utility.DecodeMpiFromBytes(_g_2_mpi, 0, ref _smp_first_message_data[0]); Utility.DecodeMpiFromBytes(_c_2_mpi, 0, ref _smp_first_message_data[1]); Utility.DecodeMpiFromBytes(_d_2_mpi, 0, ref _smp_first_message_data[2]); Utility.DecodeMpiFromBytes(_g_3_mpi, 0, ref _smp_first_message_data[3]); Utility.DecodeMpiFromBytes(_c_3_mpi, 0, ref _smp_first_message_data[4]); Utility.DecodeMpiFromBytes(_d_3_mpi, 0, ref _smp_first_message_data[5]); if (IsValidValue(_smp_first_message_data[0]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: G2a is not valid"; ResetData(); return null; } if (IsValidValue(_smp_first_message_data[3]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage3: G3a is not valid"; ResetData(); return null; } if (VerifyZKProof(1, _smp_first_message_data[0], _smp_first_message_data[1], _smp_first_message_data[2]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: Zero-Knowledge Proof (Prefix 1) failed"; ResetData(); return null; } if (VerifyZKProof(2, _smp_first_message_data[3], _smp_first_message_data[4], _smp_first_message_data[5]) == false) { smp_event_type = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage1: Zero-Knowledge Proof (Prefix 2) failed"; ResetData(); return null; } _smp_state = OTR_SMP_STATE.EXPECT_3; smp_event_type = OTR.Interface.OTR_SMP_EVENT.SEND; return FormatSMPMessage2(); }