int com_mac_block_compute_impl(char* arg, int update) { char* block_str = strtok(arg, " "); if (!block_str) { printf("Too few arguments: #block\n"); return -1; } unsigned int block = (unsigned int) strtoul(block_str, &block_str, 16); if (*block_str != '\0') { printf("Invalid block character (non hex): %s\n", block_str); return -1; } if (block < 0 || block > 0xff) { printf("Invalid block [0,ff]: %x\n", block); return -1; } // Use the key unsigned char* mac = compute_block_mac(block, current_mac_key, update); // MAC is null on error, else 8 bytes if (mac == 0) return -1; // Only need 16 MSBs. printf("Block %2.2x, MAC : ", block); print_hex_array_sep(mac, 2, " "); printf("\n"); return 0; }
int com_mac_validate(char* arg) { char* a = strtok(arg, " "); if (a && strtok(NULL, " ") != (char*)NULL) { printf("Too many arguments\n"); return -1; } mf_size_t size = parse_size_default(a, MF_1K); if (size == MF_INVALID_SIZE) { printf("Unknown argument: %s\n", a); return -1; } for(size_t i = 1; i < block_count(size); i++) { if(is_trailer_block(i)) { continue; } unsigned char* mac = compute_block_mac(i, current_mac_key, 0); printf("Block: %2x ", i); printf("Tag: "); print_hex_array_sep(¤t_tag.amb[i].mbd.abtData[14], 2, " "); printf(" Computed: "); print_hex_array_sep(mac, 2, " "); printf(" Result: "); if(memcmp(mac, ¤t_tag.amb[i].mbd.abtData[14], 2) == 0) { printf("VALID"); } else { printf("IN-VALID"); } printf("\n"); } return 0; }
int com_mac_key_get_set(char* arg) { char* key_str = strtok(arg, " "); if (key_str == 0) { printf("Current MAC key: \n"); print_hex_array_sep(current_mac_key, 8, " "); printf("\n"); return 0; } uint8_t key[8]; int key_ptr = 0; // Consume the key tokens do { long int byte = strtol(key_str, &key_str, 16); if (*key_str != '\0') { printf("Invalid key character (non hex): %s\n", key_str); return -1; } if (byte < 0 || byte > 0xff) { printf("Invalid byte value [0,ff]: %lx\n", byte); return -1; } if (key_ptr > sizeof(key)) { printf("Too many bytes specified in key (should be 8).\n"); return -1; } // Accept the byte and add it to the key key[key_ptr++] = (uint8_t)byte; } while((key_str = strtok(NULL, " ")) != (char*)NULL); if (key_ptr != sizeof(key)) { printf("Too few bytes specified in key (should be 8).\n"); return -1; } // Everything ok, so update the global memcpy(current_mac_key, key, 8); return 0; }
void print_hex_array(const unsigned char* data, size_t nbytes) { print_hex_array_sep(data, nbytes, NULL); }