def handle_structures_table(self, table, tpm_alg_ids): # handle TPMS_EMPTY structure if "TPMS_EMPTY" in table.short_name: self.tpm_types_h_file_content += "typedef BYTE TPMS_EMPTY;\n\n" self.marshaller.handle_structures_table_empty_structure(table) return # SELECTOR STARTBLOCK START selector = utils.find_alg_dep(table.short_name) if selector: self.tpm_types_h_file_content += "#ifdef TPM_ALG_" + selector + "\n" # SELECTOR STARTBLOCK END typedef_name = utils.find_tpm_type_name(table.short_name) if "Structure" in table.short_name: if "TPM2B" in table.short_name: # TPM2B TYPEDEF UNION STRUCT self.handle_typedef_union_struct(table, typedef_name) else: # TYPEDEF STRUCT self.handle_typedef_struct(table, typedef_name) if "Union" in table.short_name: # TYPEDEF UNION self.handle_typedef_union(table, typedef_name, tpm_alg_ids) # SELECTOR ENDBLOCK START if selector: self.tpm_types_h_file_content += "#endif // TPM_ALG_" + selector + "\n"
def handle_bits_table(self, table): typedef_struct_name = utils.find_tpm_type_name(table.short_name) self.tpm_types_h_file_content += "typedef union {\n" self.tpm_types_h_file_content += " struct {\n" size = 0 for row in table.rows: bit = row[0] bitname = row[1] if "#" in bitname: continue nr_of_bits = 1 if ":" in bit: values = bit.split(":") from_value = int(values[1]) to_value = int(values[0]) nr_of_bits = to_value - from_value + 1 if "Reserved" in bitname: bitname = "Reserved_from_" + str(size) self.tpm_types_h_file_content += " unsigned " + bitname + utils.indent(bitname) + ": " + str(nr_of_bits) + ";\n" size += nr_of_bits # end of loop - for row in table.rows: self.tpm_types_h_file_content += " };\n" self.tpm_types_h_file_content += " UINT" + str(size) + " val;\n" self.tpm_types_h_file_content += "} " + typedef_struct_name + ";\n" self.marshaller.handle_bits_table(table)
def handle_enum_table(self, table): base_type = utils.find_tpm_base_type_name(table.short_name) new_type = utils.find_tpm_type_name(table.short_name) self.tpm_types_h_file_content += "typedef " + base_type + utils.indent( base_type) + new_type + ";\n" for row in table.rows: name = row[0] value = row[1].replace(" ", "") if name is u'' or "reserved" in name or "#" in name: # skip those lines continue # write the define statement self.tpm_types_h_file_content += "#define " + name + utils.indent( name) + "(" + new_type + ")(" + value + ")\n" # additionally write the TPM_RCS_* define if "RC_FMT1+" in value: name = name.replace("TPM_RC_", "TPM_RCS_") self.tpm_types_h_file_content += "#define " + name + utils.indent( name) + "(" + new_type + ")(" + value + ")\n" # end of loop - for row in table.rows: self.marshaller.handle_advanced_type_structures(table)
def handle_interface_table(self, table, tpm_alg_ids): base_type = utils.find_tpm_base_type_name(table.short_name) new_type = utils.find_tpm_type_name(table.short_name) typedef_statement = "typedef " + base_type + utils.indent(base_type) + new_type + ";\n" alg_dep = utils.find_alg_constraints(table.name) if alg_dep: alg_name = "TPM_ALG_" + alg_dep typedef_statement_alg_dep = "" # check for !ALG alg_ids_list = utils.expand_alg_macro(new_type, tpm_alg_ids, alg_dep) # if new type name contains algorithm type, create type definition for all corresponding IDs for alg in alg_ids_list: alg_name = alg.name final_new_type = utils.replace_alg_placeholder(new_type, alg.short_name) typedef_statement_alg_dep += "#ifdef " + alg_name + "\n" # for each algorithm typedef_statement_alg_dep += "typedef " + base_type + utils.indent(base_type) + final_new_type + ";\n" typedef_statement_alg_dep += "#endif // " + alg_name + "\n" # for each algorithm # in case there is no !ALG, create type definition for type found in table name if not typedef_statement_alg_dep: typedef_statement_alg_dep = "#ifdef " + alg_name + "\n" typedef_statement_alg_dep += "typedef " + base_type + utils.indent(base_type) + new_type + ";\n" typedef_statement_alg_dep += "#endif // " + alg_name + "\n" typedef_statement = typedef_statement_alg_dep self.tpm_types_h_file_content += typedef_statement self.marshaller.handle_interface_table(table, tpm_alg_ids)
def handle_bits_table(self, table): self.create_header_comment(table) base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) reserved = 0 i = 0 for row in table.rows: bit_idx = row[0] bit_name = row[1] bits = 1 if ":" in bit_idx and "Reserved" in bit_name: values = bit_idx.split(":") from_value = int(values[1]) to_value = int(values[0]) for b in range(from_value, to_value+1): reserved |= (1 << b) elif "Reserved" in bit_name: reserved |= (1 << i) i += bits # end of loop - for row in table.rows: # generate a bit mask starting with 0x... reserved = "0x%0.2X" % reserved result = re.search("(int|INT)([1368][246]*)", base_type) if result: size = result.group(2) elif base_type in tpm2_partx_type_mapping.dictionary.keys() and tpm2_partx_type_mapping.dictionary[base_type]: size = tpm2_partx_type_mapping.dictionary[base_type][0] else: return # in if base_type in tpm2_partx_type_mapping.dictionary.keys() and reserved == "0x00": self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format(current_type) self.content_fp += self.bits_table_marshaller.create_unmarshal_fp_define(current_type, base_type) else: self.content += self.bits_table_marshaller.create_unmarshal_code(current_type, base_type, reserved, None) self.content_fp += self.bits_table_marshaller.create_unmarshal_fp(current_type) # out if base_type in tpm2_partx_type_mapping.dictionary.keys(): self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format(current_type) self.content_fp += self.bits_table_marshaller.create_marshal_fp_define(current_type, base_type) else: self.content += self.bits_table_marshaller.create_marshal_code(current_type, size) self.content_fp += self.bits_table_marshaller.create_marshal_fp(current_type) tpm2_partx_type_mapping.dictionary[current_type] = [base_type, table.number]
def handle_advanced_type_structures(self, table): self.create_header_comment(table) base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) config = utils.find_config(table.name) size = utils.find_base_type_size(base_type) if not size: return in_declaration = re.search('([iI])', config) out_declaration = re.search('([oO])', config) s_declaration = re.search('([S])', config) cases = [] rc_fail = None for row in table.rows: name = row[0] if "#" in name: rc_fail = name.replace("#", "") elif "TPM_CAP_FIRST" in name or "TPM_CAP_LAST" in name: continue # skip else: cases.append(name) # in if in_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format(current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format(current_type) else: if (base_type in tpm2_partx_type_mapping.dictionary.keys() or s_declaration) and not rc_fail: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format(current_type) self.content_fp += self.advanced_marshaller.create_unmarshal_fp_define(current_type, base_type) else: self.content += self.advanced_marshaller.create_unmarshal_code(current_type, base_type, cases,rc_fail) self.content_fp += self.advanced_marshaller.create_unmarshal_fp(current_type) # out if out_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format(current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format(current_type) else: if base_type in tpm2_partx_type_mapping.dictionary.keys() or s_declaration: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format(current_type) self.content_fp += self.advanced_marshaller.create_marshal_fp_define(current_type, base_type) else: self.content += self.advanced_marshaller.create_marshal_code(current_type, size) self.content_fp += self.advanced_marshaller.create_marshal_fp(current_type) tpm2_partx_type_mapping.dictionary[current_type] = [base_type, table.number]
def handle_union_table(self, table, tpm_alg_ids): self.create_header_comment(table) current_type = utils.find_tpm_type_name(table.short_name) alg_dep = re.search('{(.*)}', table.short_name) if alg_dep: alg_dep = alg_dep.group(1) result = re.search('<(.*)>', table.name) if result: config = result.group(1) else: config = "IN/OUT" in_declaration = re.search('([iI])', config) out_declaration = re.search('([oO])', config) if alg_dep: self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_dep) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_dep) # in if in_declaration: self.content += self.union_table_marshaller.create_unmarshal_code( current_type, table.rows, tpm_alg_ids, self.function_prototypes_with_flag) self.content_fp += self.union_table_marshaller.create_unmarshal_fp( current_type) else: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format( current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format( current_type) # out if out_declaration: self.content += self.union_table_marshaller.create_marshal_code( current_type, table.rows, tpm_alg_ids, self.function_prototypes_with_flag) self.content_fp += self.union_table_marshaller.create_marshal_fp( current_type) else: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format( current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format( current_type) if alg_dep: self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_dep) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_dep)
def handle_simple_constants(self, table): self.create_header_comment(table) base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) alg_dep = utils.find_alg_dep(table.short_name) size = utils.find_base_type_size(base_type) if not size: return if alg_dep: self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_dep) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_dep) already_marshaled = base_type in tpm2_partx_type_mapping.dictionary.keys( ) if already_marshaled: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format( current_type) self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format( current_type) self.content_fp += self.simple_marshaller.create_unmarshal_fp_define( current_type, base_type) self.content_fp += self.simple_marshaller.create_marshal_fp_define( current_type, base_type) else: self.content += self.simple_marshaller.create_unmarshal_code( current_type, int(size) / 8) self.content += self.simple_marshaller.create_marshal_code( current_type, int(size) / 8) self.content_fp += self.simple_marshaller.create_unmarshal_fp( current_type) self.content_fp += self.simple_marshaller.create_marshal_fp( current_type) tpm2_partx_type_mapping.dictionary[current_type] = [ base_type, table.number ] if alg_dep: self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_dep) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_dep)
def handle_union_table(self, table, tpm_alg_ids): self.create_header_comment(table) current_type = utils.find_tpm_type_name(table.short_name) alg_dep = re.search('{(.*)}', table.short_name) if alg_dep: alg_dep = alg_dep.group(1) result = re.search('<(.*)>', table.name) if result: config = result.group(1) else: config = "IN/OUT" in_declaration = re.search('([iI])', config) out_declaration = re.search('([oO])', config) if alg_dep: self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_dep) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_dep) # in if in_declaration: self.content += self.union_table_marshaller.create_unmarshal_code( current_type, table.rows, tpm_alg_ids, self.function_prototypes_with_flag) self.content_fp += self.union_table_marshaller.create_unmarshal_fp(current_type) else: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format(current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format(current_type) # out if out_declaration: self.content += self.union_table_marshaller.create_marshal_code( current_type, table.rows, tpm_alg_ids, self.function_prototypes_with_flag) self.content_fp += self.union_table_marshaller.create_marshal_fp(current_type) else: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format(current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format(current_type) if alg_dep: self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_dep) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_dep)
def handle_bits_table(self, table): typedef_struct_name = utils.find_tpm_type_name(table.short_name) self.tpm_types_h_file_content += "typedef union {\n" self.tpm_types_h_file_content += " struct {\n" size = 0 for row in table.rows: bit = row[0] bitname = row[1] if "#" in bitname: continue nr_of_bits = 1 if ":" in bit: values = bit.split(":") from_value = int(values[1]) to_value = int(values[0]) nr_of_bits = to_value - from_value + 1 if "Reserved" in bitname: bitname = "Reserved_from_" + str(size) pos = bitname.find(" ".encode()) if pos != -1 and (bitname[pos - 1] in string.ascii_lowercase or bitname[pos - 1] in string.ascii_uppercase): if (bitname[pos + 1] in string.ascii_lowercase or bitname[pos + 1] in string.ascii_uppercase): bitname = bitname.replace(" ".encode(), "".encode()) self.tpm_types_h_file_content += " unsigned " + bitname + utils.indent( bitname) + ": " + str(nr_of_bits) + ";\n" size += nr_of_bits # end of loop - for row in table.rows: self.tpm_types_h_file_content += " };\n" self.tpm_types_h_file_content += " UINT" + str(size) + " val;\n" self.tpm_types_h_file_content += "} " + typedef_struct_name + ";\n" self.marshaller.handle_bits_table(table)
def handle_enum_table(self, table): base_type = utils.find_tpm_base_type_name(table.short_name) new_type = utils.find_tpm_type_name(table.short_name) self.tpm_types_h_file_content += "typedef " + base_type + utils.indent(base_type) + new_type + ";\n" for row in table.rows: name = row[0] value = row[1].replace(" ", "") if name is u'' or "reserved" in name or "#" in name: # skip those lines continue # write the define statement self.tpm_types_h_file_content += "#define " + name + utils.indent(name) + "(" + new_type + ")(" + value + ")\n" # additionally write the TPM_RCS_* define if "RC_FMT1+" in value: name = name.replace("TPM_RC_", "TPM_RCS_") self.tpm_types_h_file_content += "#define " + name + utils.indent(name) + "(" + new_type + ")(" + value + ")\n" # end of loop - for row in table.rows: self.marshaller.handle_advanced_type_structures(table)
def handle_simple_constants(self, table): self.create_header_comment(table) base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) alg_dep = utils.find_alg_dep(table.short_name) size = utils.find_base_type_size(base_type) if not size: return if alg_dep: self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_dep) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_dep) already_marshaled = base_type in tpm2_partx_type_mapping.dictionary.keys() if already_marshaled: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format(current_type) self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format(current_type) self.content_fp += self.simple_marshaller.create_unmarshal_fp_define(current_type, base_type) self.content_fp += self.simple_marshaller.create_marshal_fp_define(current_type, base_type) else: self.content += self.simple_marshaller.create_unmarshal_code(current_type, int(size)/8) self.content += self.simple_marshaller.create_marshal_code(current_type, int(size)/8) self.content_fp += self.simple_marshaller.create_unmarshal_fp(current_type) self.content_fp += self.simple_marshaller.create_marshal_fp(current_type) tpm2_partx_type_mapping.dictionary[current_type] = [base_type, table.number] if alg_dep: self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_dep) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_dep)
def handle_interface_table(self, table, tpm_alg_ids): base_type = utils.find_tpm_base_type_name(table.short_name) new_type = utils.find_tpm_type_name(table.short_name) typedef_statement = "typedef " + base_type + utils.indent( base_type) + new_type + ";\n" alg_dep = utils.find_alg_constraints(table.name) if alg_dep: alg_name = "TPM_ALG_" + alg_dep typedef_statement_alg_dep = "" # check for !ALG alg_ids_list = utils.expand_alg_macro(new_type, tpm_alg_ids, alg_dep) # if new type name contains algorithm type, create type definition for all corresponding IDs for alg in alg_ids_list: alg_name = alg.name final_new_type = utils.replace_alg_placeholder( new_type, alg.short_name) typedef_statement_alg_dep += "#ifdef " + alg_name + "\n" # for each algorithm typedef_statement_alg_dep += "typedef " + base_type + utils.indent( base_type) + final_new_type + ";\n" typedef_statement_alg_dep += "#endif // " + alg_name + "\n" # for each algorithm # in case there is no !ALG, create type definition for type found in table name if not typedef_statement_alg_dep: typedef_statement_alg_dep = "#ifdef " + alg_name + "\n" typedef_statement_alg_dep += "typedef " + base_type + utils.indent( base_type) + new_type + ";\n" typedef_statement_alg_dep += "#endif // " + alg_name + "\n" typedef_statement = typedef_statement_alg_dep self.tpm_types_h_file_content += typedef_statement self.marshaller.handle_interface_table(table, tpm_alg_ids)
def handle_bits_table(self, table): self.create_header_comment(table) base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) reserved = 0 i = 0 for row in table.rows: bit_idx = row[0] bit_name = row[1] bits = 1 if ":" in bit_idx and "Reserved" in bit_name: values = bit_idx.split(":") from_value = int(values[1]) to_value = int(values[0]) for b in range(from_value, to_value + 1): reserved |= (1 << b) elif "Reserved" in bit_name: reserved |= (1 << i) i += bits # end of loop - for row in table.rows: # generate a bit mask starting with 0x... reserved = "0x%0.2X" % reserved result = re.search("(int|INT)([1368][246]*)", base_type) if result: size = result.group(2) elif base_type in tpm2_partx_type_mapping.dictionary.keys( ) and tpm2_partx_type_mapping.dictionary[base_type]: size = tpm2_partx_type_mapping.dictionary[base_type][0] else: return # in if base_type in tpm2_partx_type_mapping.dictionary.keys( ) and reserved == "0x00": self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format( current_type) self.content_fp += self.bits_table_marshaller.create_unmarshal_fp_define( current_type, base_type) else: self.content += self.bits_table_marshaller.create_unmarshal_code( current_type, base_type, reserved, None) self.content_fp += self.bits_table_marshaller.create_unmarshal_fp( current_type) # out if base_type in tpm2_partx_type_mapping.dictionary.keys(): self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format( current_type) self.content_fp += self.bits_table_marshaller.create_marshal_fp_define( current_type, base_type) else: self.content += self.bits_table_marshaller.create_marshal_code( current_type, size) self.content_fp += self.bits_table_marshaller.create_marshal_fp( current_type) tpm2_partx_type_mapping.dictionary[current_type] = [ base_type, table.number ]
def handle_interface_table(self, table, tpm_alg_ids): self.create_header_comment(table) bool_flag = False base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) # config block begin config = re.search('<(.*)>', table.name) if config: config = config.group(1) else: config = "IN/OUT" in_declaration = re.search('([iI])', config) out_declaration = re.search('([oO])', config) # s_declaration = re.search('([S])', config) # config block end # distinguish between a normal case, when a simple table needs to be processed # and a case, where a table represents multiple tables (see table 124) alg_dep = None if "!" not in current_type: result = re.search('\{(.*)\}', table.name) if result: alg_dep = result.group(1) cases = [] alg_list = [] checks = [] conditions = None rc_fail = None for row in table.rows: value_name = row[0] if "{" in value_name: values = value_name.split(":") from_value = values[0].replace("{", "") to_value = values[1].replace("}", "") checks.append([from_value.strip(), to_value.strip()]) elif "+TPM" in value_name: conditions = value_name.replace("+", "") self.function_prototypes_with_flag[current_type] = "" bool_flag = True elif "$RSA_KEY_SIZES_BITS" in value_name: cases = constants.RSA_KEY_SIZES_BITS elif "$ECC_CURVES" in value_name: cases = constants.ECC_CURVES elif "#" not in value_name: if "$!ALG" in value_name: alg_list += utils.expand_alg_macro(value_name, tpm_alg_ids, alg_dep) elif "!ALG" in value_name: alg_list += utils.expand_alg_macro(value_name, tpm_alg_ids, alg_dep) else: cases.append(value_name) else: rc_fail = value_name.replace("#", "") # end of loop - for row in table.rows: if "!ALG.S" in current_type: # interpret the table as multiple tables for alg in alg_list: # symmetric algorithm in question alg_name = re.search(r'TPM_ALG_(.*)', alg.name) alg_name = alg_name.group(1) # substitute '!ALG.S' part in the value with the ALG name val = re.sub(r'!ALG\.S', alg_name, current_type) self.content += "// Table 2:" + table.number + " - " \ + table.short_name.replace("{!ALG.S} (TPM_KEY_BITS) ", "").replace("!ALG.S", alg_name) \ + " (" + table.table_type + ")\n" self.content_fp += "// Table 2:" + table.number + " - " \ + table.short_name.replace("{!ALG.S} (TPM_KEY_BITS) ", "").replace("!ALG.S", alg_name) \ + " (" + table.table_type + ")\n" # manually set individual alg_deps self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_name) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_name) # utilize cases for individual sizes of the supported ALG sizes cases = constants.S_KEY_SIZES[alg.name] self.content += self.interface_table_marshaller.create_unmarshal_code( val, base_type, cases, None, checks, conditions, rc_fail) self.content_fp += self.interface_table_marshaller.create_unmarshal_fp( val) # out self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format( val) self.content_fp += self.interface_table_marshaller.create_marshal_fp_define( val, base_type) # manually set individual alg_deps self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_name) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_name) # end of loop - for alg in alg_list: else: if alg_dep: self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_dep) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_dep) # in if in_declaration: self.content += self.interface_table_marshaller.create_unmarshal_code( current_type, base_type, cases, alg_list, checks, conditions, rc_fail) self.content_fp += self.interface_table_marshaller.create_unmarshal_fp( current_type, bool_flag) # out if out_declaration: if base_type in tpm2_partx_type_mapping.dictionary.keys(): self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format( current_type) self.content_fp += self.interface_table_marshaller.create_marshal_fp_define( current_type, base_type) else: self.content += self.interface_table_marshaller.create_marshal_code( current_type, base_type) self.content_fp += self.interface_table_marshaller.create_marshal_fp( current_type) if alg_dep: self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_dep) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_dep) tpm2_partx_type_mapping.dictionary[current_type] = [ base_type, table.number ]
def handle_interface_table(self, table, tpm_alg_ids): self.create_header_comment(table) bool_flag = False base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) # config block begin config = re.search('<(.*)>', table.name) if config: config = config.group(1) else: config = "IN/OUT" in_declaration = re.search('([iI])', config) out_declaration = re.search('([oO])', config) # s_declaration = re.search('([S])', config) # config block end # distinguish between a normal case, when a simple table needs to be processed # and a case, where a table represents multiple tables (see table 124) alg_dep = None if "!" not in current_type: result = re.search('\{(.*)\}', table.name) if result: alg_dep = result.group(1) cases = [] alg_list = [] checks = [] conditions = None rc_fail = None for row in table.rows: value_name = row[0] if "{" in value_name: values = value_name.split(":") from_value = values[0].replace("{", "") to_value = values[1].replace("}", "") checks.append([from_value.strip(), to_value.strip()]) elif "+TPM" in value_name: conditions = value_name.replace("+", "") self.function_prototypes_with_flag[current_type] = "" bool_flag = True elif "$RSA_KEY_SIZES_BITS" in value_name: cases = constants.RSA_KEY_SIZES_BITS elif "$ECC_CURVES" in value_name: cases = constants.ECC_CURVES elif "#" not in value_name: if "$!ALG" in value_name: alg_list += utils.expand_alg_macro(value_name, tpm_alg_ids, alg_dep) elif "!ALG" in value_name: alg_list += utils.expand_alg_macro(value_name, tpm_alg_ids, alg_dep) else: cases.append(value_name) else: rc_fail = value_name.replace("#", "") # end of loop - for row in table.rows: if "!ALG.S" in current_type: # interpret the table as multiple tables for alg in alg_list: # symmetric algorithm in question alg_name = re.search(r'TPM_ALG_(.*)', alg.name) alg_name = alg_name.group(1) # substitute '!ALG.S' part in the value with the ALG name val = re.sub(r'!ALG\.S', alg_name, current_type) self.content += "// Table 2:" + table.number + " - " \ + table.short_name.replace("{!ALG.S} (TPM_KEY_BITS) ", "").replace("!ALG.S", alg_name) \ + " (" + table.table_type + ")\n" self.content_fp += "// Table 2:" + table.number + " - " \ + table.short_name.replace("{!ALG.S} (TPM_KEY_BITS) ", "").replace("!ALG.S", alg_name) \ + " (" + table.table_type + ")\n" # manually set individual alg_deps self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_name) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_name) # utilize cases for individual sizes of the supported ALG sizes cases = constants.S_KEY_SIZES[alg.name] self.content += self.interface_table_marshaller.create_unmarshal_code( val, base_type, cases, None, checks, conditions, rc_fail) self.content_fp += self.interface_table_marshaller.create_unmarshal_fp(val) # out self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format(val) self.content_fp += self.interface_table_marshaller.create_marshal_fp_define(val, base_type) # manually set individual alg_deps self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_name) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_name) # end of loop - for alg in alg_list: else: if alg_dep: self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_dep) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_dep) # in if in_declaration: self.content += self.interface_table_marshaller.create_unmarshal_code( current_type, base_type, cases, alg_list, checks, conditions, rc_fail) self.content_fp += self.interface_table_marshaller.create_unmarshal_fp(current_type, bool_flag) # out if out_declaration: if base_type in tpm2_partx_type_mapping.dictionary.keys(): self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format(current_type) self.content_fp += self.interface_table_marshaller.create_marshal_fp_define( current_type, base_type) else: self.content += self.interface_table_marshaller.create_marshal_code(current_type, base_type) self.content_fp += self.interface_table_marshaller.create_marshal_fp(current_type) if alg_dep: self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_dep) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_dep) tpm2_partx_type_mapping.dictionary[current_type] = [base_type, table.number]
def handle_advanced_type_structures(self, table): self.create_header_comment(table) base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) config = utils.find_config(table.name) size = utils.find_base_type_size(base_type) if not size: return in_declaration = re.search('([iI])', config) out_declaration = re.search('([oO])', config) s_declaration = re.search('([S])', config) cases = [] rc_fail = None for row in table.rows: name = row[0] if "#" in name: rc_fail = name.replace("#", "") elif "TPM_CAP_FIRST" in name or "TPM_CAP_LAST" in name: continue # skip else: cases.append(name) # in if in_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format( current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format( current_type) else: if (base_type in tpm2_partx_type_mapping.dictionary.keys() or s_declaration) and not rc_fail: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format( current_type) self.content_fp += self.advanced_marshaller.create_unmarshal_fp_define( current_type, base_type) else: self.content += self.advanced_marshaller.create_unmarshal_code( current_type, base_type, cases, rc_fail) self.content_fp += self.advanced_marshaller.create_unmarshal_fp( current_type) # out if out_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format( current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format( current_type) else: if base_type in tpm2_partx_type_mapping.dictionary.keys( ) or s_declaration: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format( current_type) self.content_fp += self.advanced_marshaller.create_marshal_fp_define( current_type, base_type) else: self.content += self.advanced_marshaller.create_marshal_code( current_type, size) self.content_fp += self.advanced_marshaller.create_marshal_fp( current_type) tpm2_partx_type_mapping.dictionary[current_type] = [ base_type, table.number ]