Mercurial > pihelp
diff main.c @ 42:082c8294c86b
get rid of hmac message padding and aes hmac
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 29 Jun 2013 10:36:41 +0800 |
parents | d07aa7644c66 |
children | 69cbf9ce72b5 |
line wrap: on
line diff
--- a/main.c Fri Jun 28 23:16:00 2013 +0800 +++ b/main.c Sat Jun 29 10:36:41 2013 +0800 @@ -16,12 +16,6 @@ #include "hmac-sha1.h" #include "aes.h" -#include "fat.h" -#include "fat_config.h" -#include "partition.h" -#include "sd_raw.h" -#include "sd_raw_config.h" - //#include "simple_ds18b20.h" //#include "onewire.h" @@ -274,123 +268,6 @@ return (unsigned char)c; } -uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry) -{ - while(fat_read_dir(dd, dir_entry)) - { - if(strcmp(dir_entry->long_name, name) == 0) - { - fat_reset_dir(dd); - return 1; - } - } - - return 0; -} - -struct fat_file_struct* -open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name) -{ - struct fat_dir_entry_struct file_entry; - if(!find_file_in_dir(fs, dd, name, &file_entry)) - return 0; - - return fat_open_file(fs, &file_entry); -} - -static uint32_t sd_serial = 0; -static char conf_start[30]; - -static void -hmac_file(const char* fn) -{ - uint8_t res; - - struct sd_raw_info disk_info; - sd_raw_get_info(&disk_info); - sd_serial = disk_info.serial; - printf_P(PSTR("serial %lx\n"), sd_serial); - - struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, 1); - - if (!partition) - { - sprintf(conf_start, "part"); - return; - } - - struct fat_fs_struct* fs = fat_open(partition); - if (!fs) - { - sprintf(conf_start, "bad fs"); - return; - } - struct fat_dir_entry_struct directory; - res = fat_get_dir_entry_of_path(fs, "/", &directory); - if (!res) - { - sprintf(conf_start, "bad direc"); - return; - } - - struct fat_dir_struct* dd = fat_open_dir(fs, &directory); - if (!dd) - { - sprintf(conf_start, "bad dd"); - return; - } - struct fat_file_struct* fd = open_file_in_dir(fs, dd, fn); - if (!fd) - { - sprintf(conf_start, "bad fd"); - return; - } - - fat_read_file(fd, (uint8_t*)conf_start, sizeof(conf_start)-1); - conf_start[sizeof(conf_start)-1] = '\0'; - - fat_close_file(fd); - fd = NULL; - fat_close_dir(dd); - dd = NULL; - fat_close(fs); - fs = NULL; - partition_close(partition); - partition = NULL; - -#if 0 - char c = 0; - char buf[512]; - for (int i = 0; i < 10; i++) - { - fat_read_file(fd, buf, sizeof(buf)); - c ^= buf[0]; - } - printf("total %d\n", c); -#endif -} - - -static void -cmd_testsd(const char *param) -{ - PORT_PI_RESET &= ~_BV(PIN_PI_RESET); - DDR_PI_RESET |= _BV(PIN_PI_RESET); - long_delay(200); - - printf_P(PSTR("about to raw init\n")); - - sd_raw_init(); - printf_P(PSTR("done raw init\n")); - hmac_file(param); - printf_P(PSTR("conf_start '%s'\n"), conf_start); - sd_raw_deinit(); - - long_delay(200); - - DDR_PI_RESET &= ~_BV(PIN_PI_RESET); -} - static void cmd_reset() __attribute__ ((noreturn)); static void cmd_reset() @@ -448,17 +325,14 @@ "oneshot (%lu)\n" "uptime %lu rem %u\n" "boot normal %hhu\n" - "disk serial %lx\n" - "disk start '%s'\n" ), watchdog_long_limit, cur_watchdog_long, long_reboot_mode, watchdog_short_limit, cur_watchdog_short, newboot_limit, cur_newboot, cur_oneshot, t.ticks, t.rem, - boot_normal_status, - sd_serial, - conf_start); + boot_normal_status + ); } static void @@ -614,10 +488,10 @@ static void cmd_hmac(const char *params) { - uint8_t indata[2+HMACLEN] = {'H', ':'}; + uint8_t indata[HMACLEN]; uint8_t outdata[HMACLEN]; uint8_t key_index; - if (parse_key(params, &key_index, &indata[2], HMACLEN) != 0) + if (parse_key(params, &key_index, indata, HMACLEN) != 0) { printf_P(PSTR("FAIL: Bad input\n")); return; @@ -636,10 +510,8 @@ static void cmd_decrypt(const char *params) { - uint8_t indata[HMACLEN+AESLEN]; // XXX - // a temporary buffer - uint8_t output[HMACLEN] = {'D', ':'}; - _Static_assert(AESLEN+2 <= sizeof(output), "sufficient output buffer"); + uint8_t indata[AESLEN]; + uint8_t output[AESLEN]; uint8_t key_index; if (parse_key(params, &key_index, indata, sizeof(indata)) != 0) { @@ -651,17 +523,9 @@ long_delay(200); #endif - // check the signature - memcpy(&output[2], &indata[HMACLEN], AESLEN); - hmac_sha1(output, avr_keys[key_index], KEYLEN*8, output, (2+AESLEN)*8); - - if (!safe_mem_eq(output, indata, HMACLEN)) { - printf_P(PSTR("FAIL: hmac mismatch\n")); - } - uint8_t tmpbuf[256]; aesInit(avr_keys[key_index], tmpbuf); - aesDecrypt(&indata[HMACLEN], NULL); + aesDecrypt(indata, NULL); printf_P(PSTR("DECRYPTED: ")); printhex(output, AESLEN, stdout);