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_typedef_table(self, table, tpm_alg_ids): table_dependence = utils.find_alg_constraints(table.short_name) if table_dependence: # if table dependence {*} found in table name self.tpm_types_h_file_content += "#ifdef TPM_ALG_" + table_dependence + "\n" for row in table.rows: base_type = row[0] name = row[1] # check for !ALG alg_ids_list = utils.expand_alg_macro(name, tpm_alg_ids, table_dependence) # if new type name contains algorithm type, create type definition for all corresponding IDs if alg_ids_list: for alg in alg_ids_list: new_type = utils.replace_alg_placeholder( name, alg.short_name) self.tpm_types_h_file_content += "typedef " + base_type + utils.indent( base_type) + new_type + ";\n" else: # else create type definition for table name self.tpm_types_h_file_content += "typedef " + base_type + utils.indent( base_type) + name + ";\n" # end of loop - for row in table.rows: if table_dependence: # if table dependence {*} found in table name self.tpm_types_h_file_content += "#endif // TPM_ALG_" + table_dependence + "\n" # end of if else - if table dependence found self.marshaller.handle_simple_type_structures_new(table, tpm_alg_ids)
def handle_typedef_table(self, table, tpm_alg_ids): table_dependence = utils.find_alg_constraints(table.short_name) if table_dependence: # if table dependence {*} found in table name self.tpm_types_h_file_content += "#ifdef TPM_ALG_" + table_dependence + "\n" for row in table.rows: base_type = row[0] name = row[1] # check for !ALG alg_ids_list = utils.expand_alg_macro(name, tpm_alg_ids, table_dependence) # if new type name contains algorithm type, create type definition for all corresponding IDs if alg_ids_list: for alg in alg_ids_list: new_type = utils.replace_alg_placeholder(name, alg.short_name) self.tpm_types_h_file_content += "typedef " + base_type + utils.indent(base_type) + new_type + ";\n" else: # else create type definition for table name self.tpm_types_h_file_content += "typedef " + base_type + utils.indent(base_type) + name + ";\n" # end of loop - for row in table.rows: if table_dependence: # if table dependence {*} found in table name self.tpm_types_h_file_content += "#endif // TPM_ALG_" + table_dependence + "\n" # end of if else - if table dependence found self.marshaller.handle_simple_type_structures_new(table, tpm_alg_ids)
def handle_typedef_union(self, table, typedef_name, tpm_alg_ids): self.tpm_types_h_file_content += "typedef union {\n" for row in table.rows: parameter = row[0] member_type = row[1].replace("+", "") selector = row[2] if member_type == "" or "null" in parameter: continue member_string = "" # check for !ALG (algorithm type) algs = utils.expand_alg_macro(parameter, tpm_alg_ids) for alg in algs: dependence = alg.dependence if len(dependence) == 0: dependence = alg.short_name alg_placeholder = selector.replace("TPM_ALG_", "") # !ALG.* base_type = utils.replace_alg_placeholder( member_type, alg.short_name.upper()) new_type = parameter.replace( alg_placeholder + "_DIGEST_SIZE", alg.short_name.upper() + "_DIGEST_SIZE").replace(" ", "") if alg_placeholder == "!ALG": # fix for Table 2:144 new_type = alg.short_name.lower() else: new_type = new_type.replace(alg_placeholder, alg.short_name.lower()) member_string += "#ifdef TPM_ALG_" + dependence + "\n" member_string += " " + base_type + utils.indent( member_type) + new_type + ";\n" member_string += "#endif // TPM_ALG_" + dependence + "\n" # end of loop - for alg in algs: # in case there is no !ALG if not member_string: member_string = " " + member_type + utils.indent( member_type) + parameter + ";\n" if selector: # enclose with ifdef + endif member_string = "#ifdef " + selector + "\n" + member_string member_string += "#endif // " + selector + "\n" # end of if - if not member_string: self.tpm_types_h_file_content += member_string # end of loop - for row in table.rows: self.tpm_types_h_file_content += "} " + typedef_name + ";\n" self.marshaller.handle_union_table(table, tpm_alg_ids)
def handle_typedef_union(self, table, typedef_name, tpm_alg_ids): self.tpm_types_h_file_content += "typedef union {\n" for row in table.rows: parameter = row[0] member_type = row[1].replace("+", "") selector = row[2] if member_type == "" or "null" in parameter: continue member_string = "" # check for !ALG (algorithm type) algs = utils.expand_alg_macro(parameter, tpm_alg_ids) for alg in algs: dependence = alg.dependence if len(dependence) == 0: dependence = alg.short_name alg_placeholder = selector.replace("TPM_ALG_", "") # !ALG.* base_type = utils.replace_alg_placeholder(member_type, alg.short_name.upper()) new_type = parameter.replace(alg_placeholder + "_DIGEST_SIZE", alg.short_name.upper() + "_DIGEST_SIZE").replace(" ", "") if alg_placeholder == "!ALG": # fix for Table 2:144 new_type = alg.short_name.lower() else: new_type = new_type.replace(alg_placeholder, alg.short_name.lower()) member_string += "#ifdef TPM_ALG_" + dependence + "\n" member_string += " " + base_type + utils.indent(member_type) + new_type + ";\n" member_string += "#endif // TPM_ALG_" + dependence + "\n" # end of loop - for alg in algs: # in case there is no !ALG if not member_string: member_string = " " + member_type + utils.indent(member_type) + parameter + ";\n" if selector: # enclose with ifdef + endif member_string = "#ifdef " + selector + "\n" + member_string member_string += "#endif // " + selector + "\n" # end of if - if not member_string: self.tpm_types_h_file_content += member_string # end of loop - for row in table.rows: self.tpm_types_h_file_content += "} " + typedef_name + ";\n" self.marshaller.handle_union_table(table, tpm_alg_ids)
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_simple_type_structures_new(self, table, tpm_alg_ids=None): self.create_header_comment(table) for row in table.rows: base_type = row[0] new_type = row[1] types_list = [] # check for !ALG alg_dep = utils.find_alg_constraints(table.short_name) algs = utils.expand_alg_macro(new_type, tpm_alg_ids, alg_dep) for alg in algs: typedef = utils.replace_alg_placeholder(new_type, alg.short_name) types_list.append(typedef) # end of loop - for alg in alg_list: # in case there is no !ALG if not algs: types_list = [new_type] # handle the (list of) typ(es) in the current row for current_type in types_list: 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) # check if type is a C base type and already in mapping dictionary base_type, c_base_type = self.handle_base_types(base_type) already_marshaled = base_type in tpm2_partx_type_mapping.dictionary.keys() unsigned_marshaled = "u"+base_type in tpm2_partx_type_mapping.dictionary.keys() if unsigned_marshaled: base_type = "u"+base_type if c_base_type or already_marshaled or unsigned_marshaled: # if original type was already marshaled somehow # marshalling new type would be redundant - change to define for new type orig_mrshl = tpm2_partx_type_mapping.dictionary[base_type] orig_table_nr = orig_mrshl[1] # recursive exploration of most basic base_type in mapping dictionary base_type = utils.find_basic_base_type(base_type) # add a comment that points to the origin of the type definition self.content += "// " + base_type + " definition used from Table 2:" + orig_table_nr + "\n" 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: # handle base types: figure out the size size = utils.find_base_type_size(base_type) if not size: # skip base type without size (i.e. 'int', which is used for 'BOOL') continue 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) if unsigned_marshaled: base_type = base_type[1:] tpm2_partx_type_mapping.dictionary[current_type] = [base_type, table.number] # done with the new 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) self.content += "\n" self.content_fp += "\n"
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_simple_type_structures_new(self, table, tpm_alg_ids=None): self.create_header_comment(table) for row in table.rows: base_type = row[0] new_type = row[1] types_list = [] # check for !ALG alg_dep = utils.find_alg_constraints(table.short_name) algs = utils.expand_alg_macro(new_type, tpm_alg_ids, alg_dep) for alg in algs: typedef = utils.replace_alg_placeholder( new_type, alg.short_name) types_list.append(typedef) # end of loop - for alg in alg_list: # in case there is no !ALG if not algs: types_list = [new_type] # handle the (list of) typ(es) in the current row for current_type in types_list: 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) # check if type is a C base type and already in mapping dictionary base_type, c_base_type = self.handle_base_types(base_type) already_marshaled = base_type in tpm2_partx_type_mapping.dictionary.keys( ) unsigned_marshaled = "u" + base_type in tpm2_partx_type_mapping.dictionary.keys( ) if unsigned_marshaled: base_type = "u" + base_type if c_base_type or already_marshaled or unsigned_marshaled: # if original type was already marshaled somehow # marshalling new type would be redundant - change to define for new type orig_mrshl = tpm2_partx_type_mapping.dictionary[base_type] orig_table_nr = orig_mrshl[1] # recursive exploration of most basic base_type in mapping dictionary base_type = utils.find_basic_base_type(base_type) # add a comment that points to the origin of the type definition self.content += "// " + base_type + " definition used from Table 2:" + orig_table_nr + "\n" 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: # handle base types: figure out the size size = utils.find_base_type_size(base_type) if not size: # skip base type without size (i.e. 'int', which is used for 'BOOL') continue 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) if unsigned_marshaled: base_type = base_type[1:] tpm2_partx_type_mapping.dictionary[current_type] = [ base_type, table.number ] # done with the new 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) self.content += "\n" self.content_fp += "\n"
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 create_unmarshal_code(self, mrshl_type, rows, tpm_alg_ids, function_prototypes_with_flag): cases_list = [] alg_list = [] algs_string = "" for row in rows: parameter = row[0] tpm_type = row[1].replace("+", "").upper() selector = row[2] if selector is None: selector = "" if "null" in parameter: cases_list.append(row) continue result = re.search("TPM_ALG_", selector) if result is not None: if selector.endswith("!ALG"): # fix for 2:144 selector = "TPM_ALG_" + parameter if "!ALG" in selector: alg_list += utils.expand_alg_macro(selector, tpm_alg_ids) else: alg_list += [tpm2_part2_structures_alg_ids.AlgorithmID(selector.upper(), "", "", "")] for alg in alg_list: full_alg_name = alg.name alg_name = utils.extract_alg_name(full_alg_name) algs_string += "#ifdef " + full_alg_name + "\n" algs_string += " case " + full_alg_name + ":\n" if "TPM2B_" in tpm_type: alg_key = selector.replace("TPM_ALG_", "").upper() tpm_type_final = tpm_type.replace(alg_key, alg_name.upper()) algs_string += MarshalTemplates.return_type_unmarshal.safe_substitute( TO_TYPE=tpm_type_final, MEMBER=parameter, TO_MEMBER="" ) elif "TPMI_" in tpm_type or "TPMS_" in tpm_type or "TPMT_" in tpm_type: alg_key = selector.replace("TPM_ALG_", "").upper() tpm_type_final = tpm_type.replace(alg_key, alg_name.upper()) # in table 144 the part that needs to be removed differs from alg_key if re.search("!ALG", tpm_type_final): tpm_type_final = tpm_type_final.replace("!ALG", alg_name.upper()) if "!ALG" in parameter: parameter_final = alg_name.lower() else: parameter_final = parameter to_member = "" if tpm_type_final in function_prototypes_with_flag.keys(): to_member = ", 0" algs_string += MarshalTemplates.return_type_unmarshal.safe_substitute( TO_TYPE=tpm_type_final, MEMBER=parameter_final, TO_MEMBER=to_member ) else: if len(tpm_type) == 0: algs_string += " " + MarshalTemplates.RETURN_SUCCESS + "\n" else: algs_string += MarshalTemplates.return_array_unmarshal.safe_substitute( TO_TYPE=tpm_type, MEMBER=alg_name.lower(), TO_MEMBER=", (INT32)" + alg_name.upper() + "_DIGEST_SIZE", ) algs_string += "#endif // " + full_alg_name + "\n" # end of loop - for alg in alg_list: alg_list = [] continue if len(selector) > 0: cases_list.append(row) # end of loop - for row in rows: # CASES cases_string = "" for case in cases_list: cases_string += " case " + case[2] + ":\n" cases_string += " " + MarshalTemplates.RETURN_SUCCESS + "\n" cases_string = cases_string[:-1] code = MarshalTemplates.TYPE_Unmarshal_union.safe_substitute( TYPE=mrshl_type, CASES=cases_string, ALGS=algs_string ) return code
def create_unmarshal_code(self, mrshl_type, rows, tpm_alg_ids, function_prototypes_with_flag): cases_list = [] alg_list = [] algs_string = "" for row in rows: parameter = row[0] tpm_type = row[1].replace("+", "").upper() selector = row[2] if selector is None: selector = "" if "null" in parameter: cases_list.append(row) continue result = re.search('TPM_ALG_', selector) if result is not None: if selector.endswith("!ALG"): # fix for 2:144 selector = "TPM_ALG_" + parameter if "!ALG" in selector: alg_list += utils.expand_alg_macro(selector, tpm_alg_ids) else: alg_list += [ tpm2_part2_structures_alg_ids.AlgorithmID( selector.upper(), "", "", "") ] for alg in alg_list: full_alg_name = alg.name alg_name = utils.extract_alg_name(full_alg_name) algs_string += "#ifdef " + full_alg_name + "\n" algs_string += " case " + full_alg_name + ":\n" if "TPM2B_" in tpm_type: alg_key = selector.replace("TPM_ALG_", "").upper() tpm_type_final = tpm_type.replace( alg_key, alg_name.upper()) algs_string += MarshalTemplates.return_type_unmarshal.safe_substitute( TO_TYPE=tpm_type_final, MEMBER=parameter, TO_MEMBER="") elif "TPMI_" in tpm_type or "TPMS_" in tpm_type or "TPMT_" in tpm_type: alg_key = selector.replace("TPM_ALG_", "").upper() tpm_type_final = tpm_type.replace( alg_key, alg_name.upper()) # in table 144 the part that needs to be removed differs from alg_key if re.search('!ALG', tpm_type_final): tpm_type_final = tpm_type_final.replace( '!ALG', alg_name.upper()) if "!ALG" in parameter: parameter_final = alg_name.lower() else: parameter_final = parameter to_member = "" if tpm_type_final in function_prototypes_with_flag.keys( ): to_member = ", 0" algs_string += MarshalTemplates.return_type_unmarshal.safe_substitute( TO_TYPE=tpm_type_final, MEMBER=parameter_final, TO_MEMBER=to_member) else: if len(tpm_type) == 0: algs_string += " " + MarshalTemplates.RETURN_SUCCESS + "\n" else: algs_string += MarshalTemplates.return_array_unmarshal.safe_substitute( TO_TYPE=tpm_type, MEMBER=alg_name.lower(), TO_MEMBER=", (INT32)" + alg_name.upper() + "_DIGEST_SIZE") algs_string += "#endif // " + full_alg_name + "\n" # end of loop - for alg in alg_list: alg_list = [] continue if len(selector) > 0: cases_list.append(row) # end of loop - for row in rows: # CASES cases_string = "" for case in cases_list: cases_string += " case " + case[2] + ":\n" cases_string += " " + MarshalTemplates.RETURN_SUCCESS + "\n" cases_string = cases_string[:-1] code = MarshalTemplates.TYPE_Unmarshal_union.safe_substitute( TYPE=mrshl_type, CASES=cases_string, ALGS=algs_string) return code