def verify(self, message, sig, i, k): lms_type, q, lmots_sig, path = LmsSerializer.deserialize_signature(sig) node_num = q + 2**self.lms_type.h if lms_type != self.lms_type: return ValueError( "LMS signature type does not match expected type") path_value = iter(path) lmots = Lmots(self.lmots_type) sig_pub_key = lmots.extract_public_key(signature=lmots_sig, s=i + u32str(q), message=message) sig_pub_key_hash = sha256_hash(i + sig_pub_key.k + u32str(node_num) + D_LEAF) while node_num > 1: if node_num % 2: sig_pub_key_hash = sha256_hash(i + path_value.next() + sig_pub_key_hash + u32str(node_num / 2) + D_INTR) else: sig_pub_key_hash = sha256_hash(i + sig_pub_key_hash + path_value.next() + u32str(node_num / 2) + D_INTR) node_num = node_num / 2 is_valid = sig_pub_key_hash == k return is_valid
def _T(self, r, pub_nodes, pub_lmots_keys, i): if r >= 2**self.lms_type.h: pub_nodes[r] = sha256_hash(i + pub_lmots_keys[r - 2**self.lms_type.h].k + u32str(r) + D_LEAF) return pub_nodes[r] else: pub_nodes[r] = sha256_hash( i + self._T(2 * r, pub_nodes, pub_lmots_keys, i) + self._T(2 * r + 1, pub_nodes, pub_lmots_keys, i) + u32str(r) + D_INTR) return pub_nodes[r]
def serialize_signature(levels, pub_list, sig_list, msg_sig): serial_sig = u32str(levels - 1) for i in xrange(0, levels - 1): serial_sig = serial_sig + sig_list[i] serial_sig = serial_sig + LmsSerializer.serialize_public_key( pub_list[i + 1]) serial_sig = serial_sig + msg_sig return serial_sig
def __str__(self): """ String representation of LMS public key object. :return: string """ s_list = list() StringFormat.line(s_list) s_list.append("LMS public key") StringFormat.format_hex(s_list, "LMS type", u32str(self.lms_type.type_code), self.lms_type.name) StringFormat.format_hex(s_list, "LMOTS_type", u32str(self.lmots_type.type_code), self.lms_type.name) StringFormat.format_hex(s_list, "I", self.i) StringFormat.format_hex(s_list, "K", self.k) StringFormat.line(s_list) return "\n".join(s_list)
def __str__(self): """ String representation of LMS signature object. :return: string """ s_list = list() StringFormat.line(s_list) s_list.append("LMS signature") # TODO: we should not be deserializing here - we should be working directly with the signature object lms_type, q, lmots_sig, path = LmsSerializer.deserialize_signature(self.signature) StringFormat.format_hex(s_list, "q", u32str(q)) # TODO: we should not be deserializing here - we should be working directly with the signature object sig = LmotsSerializer.deserialize_signature(lmots_sig) s_list.append(str(sig)) StringFormat.format_hex(s_list, "LMS type", u32str(lms_type.type_code), lms_type.name) for i, e in enumerate(path): StringFormat.format_hex(s_list, "path[" + str(i) + "]", e) return "\n".join(s_list)
def deserialize_print_hex(hex_value): lms_type = LmsSerializer.get_lms_type(hex_value) lmots_type = LmsSerializer.get_lmots_type(hex_value) StringFormat.line() print "LMS private key" seed = hex_value[8:8 + lmots_type.n] i = hex_value[8 + lmots_type.n:8 + lmots_type.n + lms_type.len_i] q = hex_u32_to_int(hex_value[8 + lmots_type.n + lms_type.len_i:8 + lmots_type.n + lms_type.len_i + 4]) StringFormat.format_hex("lms_type", u32str(lms_type)) StringFormat.format_hex("lmots_type", u32str(lmots_type)) StringFormat.format_hex("seed", seed) StringFormat.format_hex("I", i) StringFormat.format_hex("leaf_num", u32str(q)) StringFormat.line()
def __str__(self): """ String representation of HSS public key object. :return: string """ s_list = list() StringFormat.line(s_list) s_list.append("HSS public key") StringFormat.format_hex(s_list, "levels", u32str(self.levels)) s_list.append(str(self.pub1)) StringFormat.line(s_list) return "\n".join(s_list)
def __str__(self): """ String representation of HSS private key object. :return: string """ s_list = list() StringFormat.line(s_list) s_list.append("HSS private key") StringFormat.format_hex(s_list, "levels", u32str(self.levels)) for prv in self.pvt_keys: s_list.append(str(prv)) StringFormat.line(s_list) return "\n".join(s_list)
def __str__(self): """ String representation of LMOTS private key object. :return: string """ s_list = list() StringFormat.line(s_list) s_list.append("LMOTS private key") StringFormat.format_hex(s_list, "LMOTS type", u32str(self.lmots_type.type_code), self.lmots_type.name) StringFormat.format_hex(s_list, "S", self.s) for i, x in enumerate(self.raw_key): StringFormat.format_hex(s_list, "x[" + str(i) + "]", x) StringFormat.line(s_list) return "\n".join(s_list)
def generate_key_pair(self, seed=None, i=None, q=0): """ Generate a LMS key pair. :param seed: seed value; if None then random bytes read from entropy source :param i: i value; if None then random bytes read from entropy source :param q: q value; if None then 0 :return: list of LMOTS public keys and list of LMOTS private keys """ if seed is not None and len(seed) != self.lmots_type.n: raise ValueError("seed length invalid", str(len(seed))) if i is not None and len(i) != self.lms_type.len_i: raise ValueError("var_id length invalid", str(len(i))) if seed is None: seed = self._entropy_source.read(self.lmots_type.n) if i is None: i = self._entropy_source.read(self.lms_type.len_i) priv = list() pub = list() # Q: instance number # w: Winternitz parameter # I: identity lmots = Lmots(lmots_type=self.lmots_type) for q in xrange(0, 2**self.lms_type.h): s = i + u32str(q) ots_pub, ots_priv = lmots.generate_key_pair(s=s, seed=seed) priv.append(ots_priv) pub.append(ots_pub) # init the lms private key object lms_pvt_key = LmsPrivateKey(lms_type=self.lms_type, lmots_type=self.lmots_type, private_keys=priv, seed=seed, i=i, q_init=q) lms_pub_key = self.rebuild_public_key(i, pub) return lms_pub_key, lms_pvt_key
def print_hss_sig(sig): levels, pub_list, sig_list, lms_sig = HssSerializer.deserialize_signature( sig) s_list = list() StringFormat.line(s_list) s_list.append("HSS signature") StringFormat.format_hex(s_list, "Nspk", u32str(levels - 1)) for i in xrange(0, levels - 1): s_list.append("sig[" + str(i) + "]: ") s_list.append(string_to_hex(sig_list[i])) s_list.append("pub[" + str(i) + "]: ") lms_type, lmots_type, i, k = LmsSerializer.deserialize_public_key( pub_list[i]) lms_pub_key = LmsPublicKey(lms_type=lms_type, lmots_type=lmots_type, i=i, k=k) s_list.append(str(lms_pub_key)) s_list.append("final_signature: ") s_list.append(string_to_hex(lms_sig)) sig_string = "\n".join(s_list) print(sig_string)
def serialize_signature(signature): return u32str(signature.q) + signature.signature + u32str(signature.type_code.type_code) \ + serialize_array(signature.path)
def serialize_private_key(private_key): return u32str( private_key.levels) + LmsSerializer.serialize_private_key( private_key.pvt_keys[0])
def serialize_public_key(public_key): return u32str(public_key.levels) + LmsSerializer.serialize_public_key( public_key.pub1)
def serialize_public_key(public_key): return u32str( public_key.lmots_type.type_code) + public_key.s + public_key.k
def serialize_signature(signature): return u32str( signature.lmots_type.type_code) + signature.c + serialize_array( signature.y)
def serialize_private_key(pvt_key): return u32str(pvt_key.lms_type.type_code) + u32str(pvt_key.lmots_type.type_code) \ + pvt_key.seed + pvt_key.i + u32str(pvt_key.leaf_num)