comparison 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
comparison
equal deleted inserted replaced
41:d07aa7644c66 42:082c8294c86b
13 #include <util/atomic.h> 13 #include <util/atomic.h>
14 #include <util/crc16.h> 14 #include <util/crc16.h>
15 15
16 #include "hmac-sha1.h" 16 #include "hmac-sha1.h"
17 #include "aes.h" 17 #include "aes.h"
18
19 #include "fat.h"
20 #include "fat_config.h"
21 #include "partition.h"
22 #include "sd_raw.h"
23 #include "sd_raw_config.h"
24 18
25 //#include "simple_ds18b20.h" 19 //#include "simple_ds18b20.h"
26 //#include "onewire.h" 20 //#include "onewire.h"
27 21
28 // not set via bootloader 22 // not set via bootloader
272 UDR0 = '\n'; 266 UDR0 = '\n';
273 } 267 }
274 return (unsigned char)c; 268 return (unsigned char)c;
275 } 269 }
276 270
277 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)
278 {
279 while(fat_read_dir(dd, dir_entry))
280 {
281 if(strcmp(dir_entry->long_name, name) == 0)
282 {
283 fat_reset_dir(dd);
284 return 1;
285 }
286 }
287
288 return 0;
289 }
290
291 struct fat_file_struct*
292 open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
293 {
294 struct fat_dir_entry_struct file_entry;
295 if(!find_file_in_dir(fs, dd, name, &file_entry))
296 return 0;
297
298 return fat_open_file(fs, &file_entry);
299 }
300
301 static uint32_t sd_serial = 0;
302 static char conf_start[30];
303
304 static void
305 hmac_file(const char* fn)
306 {
307 uint8_t res;
308
309 struct sd_raw_info disk_info;
310 sd_raw_get_info(&disk_info);
311 sd_serial = disk_info.serial;
312 printf_P(PSTR("serial %lx\n"), sd_serial);
313
314 struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, 1);
315
316 if (!partition)
317 {
318 sprintf(conf_start, "part");
319 return;
320 }
321
322 struct fat_fs_struct* fs = fat_open(partition);
323 if (!fs)
324 {
325 sprintf(conf_start, "bad fs");
326 return;
327 }
328 struct fat_dir_entry_struct directory;
329 res = fat_get_dir_entry_of_path(fs, "/", &directory);
330 if (!res)
331 {
332 sprintf(conf_start, "bad direc");
333 return;
334 }
335
336 struct fat_dir_struct* dd = fat_open_dir(fs, &directory);
337 if (!dd)
338 {
339 sprintf(conf_start, "bad dd");
340 return;
341 }
342 struct fat_file_struct* fd = open_file_in_dir(fs, dd, fn);
343 if (!fd)
344 {
345 sprintf(conf_start, "bad fd");
346 return;
347 }
348
349 fat_read_file(fd, (uint8_t*)conf_start, sizeof(conf_start)-1);
350 conf_start[sizeof(conf_start)-1] = '\0';
351
352 fat_close_file(fd);
353 fd = NULL;
354 fat_close_dir(dd);
355 dd = NULL;
356 fat_close(fs);
357 fs = NULL;
358 partition_close(partition);
359 partition = NULL;
360
361 #if 0
362 char c = 0;
363 char buf[512];
364 for (int i = 0; i < 10; i++)
365 {
366 fat_read_file(fd, buf, sizeof(buf));
367 c ^= buf[0];
368 }
369 printf("total %d\n", c);
370 #endif
371 }
372
373
374 static void
375 cmd_testsd(const char *param)
376 {
377 PORT_PI_RESET &= ~_BV(PIN_PI_RESET);
378 DDR_PI_RESET |= _BV(PIN_PI_RESET);
379 long_delay(200);
380
381 printf_P(PSTR("about to raw init\n"));
382
383 sd_raw_init();
384 printf_P(PSTR("done raw init\n"));
385 hmac_file(param);
386 printf_P(PSTR("conf_start '%s'\n"), conf_start);
387 sd_raw_deinit();
388
389 long_delay(200);
390
391 DDR_PI_RESET &= ~_BV(PIN_PI_RESET);
392 }
393
394 static void cmd_reset() __attribute__ ((noreturn)); 271 static void cmd_reset() __attribute__ ((noreturn));
395 static void 272 static void
396 cmd_reset() 273 cmd_reset()
397 { 274 {
398 printf_P(PSTR("reset\n")); 275 printf_P(PSTR("reset\n"));
446 "watchdog_short %lu (%lu)\n" 323 "watchdog_short %lu (%lu)\n"
447 "newboot %lu (%lu)\n" 324 "newboot %lu (%lu)\n"
448 "oneshot (%lu)\n" 325 "oneshot (%lu)\n"
449 "uptime %lu rem %u\n" 326 "uptime %lu rem %u\n"
450 "boot normal %hhu\n" 327 "boot normal %hhu\n"
451 "disk serial %lx\n"
452 "disk start '%s'\n"
453 ), 328 ),
454 watchdog_long_limit, cur_watchdog_long, long_reboot_mode, 329 watchdog_long_limit, cur_watchdog_long, long_reboot_mode,
455 watchdog_short_limit, cur_watchdog_short, 330 watchdog_short_limit, cur_watchdog_short,
456 newboot_limit, cur_newboot, 331 newboot_limit, cur_newboot,
457 cur_oneshot, 332 cur_oneshot,
458 t.ticks, t.rem, 333 t.ticks, t.rem,
459 boot_normal_status, 334 boot_normal_status
460 sd_serial, 335 );
461 conf_start);
462 } 336 }
463 337
464 static void 338 static void
465 cmd_set_params(const char *params) 339 cmd_set_params(const char *params)
466 { 340 {
612 } 486 }
613 487
614 static void 488 static void
615 cmd_hmac(const char *params) 489 cmd_hmac(const char *params)
616 { 490 {
617 uint8_t indata[2+HMACLEN] = {'H', ':'}; 491 uint8_t indata[HMACLEN];
618 uint8_t outdata[HMACLEN]; 492 uint8_t outdata[HMACLEN];
619 uint8_t key_index; 493 uint8_t key_index;
620 if (parse_key(params, &key_index, &indata[2], HMACLEN) != 0) 494 if (parse_key(params, &key_index, indata, HMACLEN) != 0)
621 { 495 {
622 printf_P(PSTR("FAIL: Bad input\n")); 496 printf_P(PSTR("FAIL: Bad input\n"));
623 return; 497 return;
624 } 498 }
625 499
634 } 508 }
635 509
636 static void 510 static void
637 cmd_decrypt(const char *params) 511 cmd_decrypt(const char *params)
638 { 512 {
639 uint8_t indata[HMACLEN+AESLEN]; // XXX 513 uint8_t indata[AESLEN];
640 // a temporary buffer 514 uint8_t output[AESLEN];
641 uint8_t output[HMACLEN] = {'D', ':'};
642 _Static_assert(AESLEN+2 <= sizeof(output), "sufficient output buffer");
643 uint8_t key_index; 515 uint8_t key_index;
644 if (parse_key(params, &key_index, indata, sizeof(indata)) != 0) 516 if (parse_key(params, &key_index, indata, sizeof(indata)) != 0)
645 { 517 {
646 printf_P(PSTR("FAIL: Bad input\n")); 518 printf_P(PSTR("FAIL: Bad input\n"));
647 return; 519 return;
649 521
650 #ifndef SIM_DEBUG 522 #ifndef SIM_DEBUG
651 long_delay(200); 523 long_delay(200);
652 #endif 524 #endif
653 525
654 // check the signature
655 memcpy(&output[2], &indata[HMACLEN], AESLEN);
656 hmac_sha1(output, avr_keys[key_index], KEYLEN*8, output, (2+AESLEN)*8);
657
658 if (!safe_mem_eq(output, indata, HMACLEN)) {
659 printf_P(PSTR("FAIL: hmac mismatch\n"));
660 }
661
662 uint8_t tmpbuf[256]; 526 uint8_t tmpbuf[256];
663 aesInit(avr_keys[key_index], tmpbuf); 527 aesInit(avr_keys[key_index], tmpbuf);
664 aesDecrypt(&indata[HMACLEN], NULL); 528 aesDecrypt(indata, NULL);
665 529
666 printf_P(PSTR("DECRYPTED: ")); 530 printf_P(PSTR("DECRYPTED: "));
667 printhex(output, AESLEN, stdout); 531 printhex(output, AESLEN, stdout);
668 putchar('\n'); 532 putchar('\n');
669 } 533 }