def createAuthKeys(self): s = pow(self.gy, self.dh.priv, DH_MODULUS) sbyte = pack_mpi(s) self.sessionId = SHA256(b'\x00' + sbyte)[:8] enc = SHA256(b'\x01' + sbyte) self.enc_c = enc[:16] self.enc_cp = enc[16:] self.mac_m1 = SHA256(b'\x02' + sbyte) self.mac_m2 = SHA256(b'\x03' + sbyte) self.mac_m1p = SHA256(b'\x04' + sbyte) self.mac_m2p = SHA256(b'\x05' + sbyte) self.extraKey = SHA256(b'\xff' + sbyte)
def proof_equal_coords(self, r, v): r1 = randrange(2, DH_MAX) r2 = randrange(2, DH_MAX) temp2 = pow(self.g1, r1, DH_MODULUS) \ * pow(self.g2, r2, DH_MODULUS) % DH_MODULUS temp1 = pow(self.g3, r1, DH_MODULUS) cb = SHA256(struct.pack(b'B', v) + pack_mpi(temp1) + pack_mpi(temp2)) c = bytes_to_long(cb) temp1 = r * c % SM_ORDER d1 = (r1-temp1) % SM_ORDER temp1 = self.secret * c % SM_ORDER d2 = (r2 - temp1) % SM_ORDER return c, d1, d2
def check_known_log(c, d, g, x, v): gd = pow(g, d, DH_MODULUS) xc = pow(x, c, DH_MODULUS) gdxc = gd * xc % DH_MODULUS return SHA256(struct.pack(b'B', v) + pack_mpi(gdxc)) == long_to_bytes(c, 32)
def proof_known_log(g, x, v): r = randrange(2, DH_MAX) c = bytes_to_long(SHA256(struct.pack(b'B', v) + pack_mpi(pow(g, r, DH_MODULUS)))) temp = x * c % SM_ORDER return c, (r-temp) % SM_ORDER