comparison main.c @ 35:cce426bb6d3e

fix rng and short watchdog
author Matt Johnston <matt@ucc.asn.au>
date Thu, 27 Jun 2013 05:40:00 +0000
parents 5d6a841eef82
children c6f77df67dde
comparison
equal deleted inserted replaced
34:b210df11f968 35:cce426bb6d3e
420 new_watchdog_short_limit, 420 new_watchdog_short_limit,
421 new_newboot_limit); 421 new_newboot_limit);
422 } 422 }
423 } 423 }
424 424
425 // returns 1 if they are equal, timing independent
426 static uint8_t
427 safe_mem_eq(const uint8_t *a, const uint8_t *b, int len)
428 {
429 uint8_t result = 0;
430 for (int i = 0; i < len; i++)
431 {
432 result |= a[i] ^ b[i];
433 }
434 return result == 0;
435 }
436
437 // returns 1 if they are equal
438 static uint8_t
439 safe_str_eq(const char *a, const char *b)
440 {
441 int la = strlen(a);
442 if (la != strlen(b))
443 {
444 return 0;
445 }
446 return safe_mem_eq((const uint8_t*)a, (const uint8_t*)b, la);
447 }
448
425 uint8_t from_hex(char c) 449 uint8_t from_hex(char c)
426 { 450 {
427 if (c >= '0' && c <= '9') { 451 if (c >= '0' && c <= '9') {
428 return c-'0'; 452 return c-'0';
429 } 453 }
556 580
557 // check the signature 581 // check the signature
558 memcpy(&output[2], &indata[HMACLEN], AESLEN); 582 memcpy(&output[2], &indata[HMACLEN], AESLEN);
559 hmac_sha1(output, avr_keys[key_index], KEYLEN*8, output, (2+AESLEN)*8); 583 hmac_sha1(output, avr_keys[key_index], KEYLEN*8, output, (2+AESLEN)*8);
560 584
561 if (memcmp(output, indata, HMACLEN) != 0) { 585 if (!safe_mem_eq(output, indata, HMACLEN)) {
562 printf_P(PSTR("FAIL: hmac mismatch\n")); 586 printf_P(PSTR("FAIL: hmac mismatch\n"));
563 } 587 }
564 588
565 uint8_t tmpbuf[256]; 589 uint8_t tmpbuf[256];
566 aesInit(avr_keys[key_index], tmpbuf); 590 aesInit(avr_keys[key_index], tmpbuf);
628 { 652 {
629 watchdog_long_count = 0; 653 watchdog_long_count = 0;
630 watchdog_short_count = 0; 654 watchdog_short_count = 0;
631 } 655 }
632 long_reboot_mode = 0; 656 long_reboot_mode = 0;
657 }
658
659 static void
660 cmd_poke()
661 {
662 printf_P(PSTR("Ah, good.\n"));
663 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
664 {
665 watchdog_short_count = 0;
666 }
633 } 667 }
634 668
635 static void 669 static void
636 cmd_vcc() 670 cmd_vcc()
637 { 671 {
655 #endif 689 #endif
656 690
657 static void 691 static void
658 cmd_prog(const char* arg) 692 cmd_prog(const char* arg)
659 { 693 {
660 if (strcmp(arg, PROG_PASSWORD) != 0) 694 if (safe_str_eq(arg, PROG_PASSWORD))
661 { 695 {
662 printf_P(PSTR("Bad prog password\n")); 696 printf_P(PSTR("Bad prog password\n"));
663 return; 697 return;
664 } 698 }
665 699
713 { 747 {
714 ADCSRA |= _BV(ADSC); 748 ADCSRA |= _BV(ADSC);
715 loop_until_bit_is_clear(ADCSRA, ADSC); 749 loop_until_bit_is_clear(ADCSRA, ADSC);
716 uint8_t low = ADCL; 750 uint8_t low = ADCL;
717 uint8_t high = ADCH; 751 uint8_t high = ADCH;
718 return (popcnt(low)&1) ^ (popcnt(high)&1); 752 uint8_t ret = (popcnt(low)&1) ^ (popcnt(high)&1);
753 return ret;
719 } 754 }
720 755
721 static void 756 static void
722 adc_random(uint8_t admux, 757 adc_random(uint8_t admux,
723 uint8_t *out, uint16_t num, uint32_t *tries) 758 uint8_t *out, uint16_t num, uint32_t *tries)
724 { 759 {
725 uint8_t ret = 0;
726 uint8_t count = 0;
727
728 PRR &= ~_BV(PRADC); 760 PRR &= ~_BV(PRADC);
729 // /16 prescaler for 691mhz, no interrupt 761 // /16 prescaler for 691mhz, no interrupt
730 ADCSRA = _BV(ADEN) | _BV(ADPS2); 762 ADCSRA = _BV(ADEN) | _BV(ADPS2);
731 763
764 ADMUX = admux;
765
732 *tries = 0; 766 *tries = 0;
733 for (int i = 0; i < num; i++) 767 for (int i = 0; i < num; i++)
734 { 768 {
769 uint8_t ret = 0;
770 uint8_t count = 0;
771
735 while (count <= 7) 772 while (count <= 7)
736 { 773 {
737 (*tries)++; 774 (*tries)++;
738 775
739 // Von Neumann extractor 776 // Von Neumann extractor
895 LOCAL_PSTR(set_key); 932 LOCAL_PSTR(set_key);
896 LOCAL_PSTR(oneshot); 933 LOCAL_PSTR(oneshot);
897 LOCAL_PSTR(hmac); 934 LOCAL_PSTR(hmac);
898 LOCAL_PSTR(decrypt); 935 LOCAL_PSTR(decrypt);
899 LOCAL_PSTR(alive); 936 LOCAL_PSTR(alive);
937 LOCAL_PSTR(poke);
900 LOCAL_PSTR(vcc); 938 LOCAL_PSTR(vcc);
901 LOCAL_PSTR(reset); 939 LOCAL_PSTR(reset);
902 LOCAL_PSTR(newboot); 940 LOCAL_PSTR(newboot);
903 LOCAL_PSTR(oldboot); 941 LOCAL_PSTR(oldboot);
904 LOCAL_PSTR(status); 942 LOCAL_PSTR(status);
918 // existence of arg_help indicates if the cmd takes a parameter. 956 // existence of arg_help indicates if the cmd takes a parameter.
919 PGM_P arg_help; 957 PGM_P arg_help;
920 } handlers[] PROGMEM = 958 } handlers[] PROGMEM =
921 { 959 {
922 {alive_str, cmd_alive, NULL}, 960 {alive_str, cmd_alive, NULL},
961 {poke_str, cmd_poke, NULL},
923 {newboot_str, cmd_newboot, NULL}, 962 {newboot_str, cmd_newboot, NULL},
924 {oldboot_str, cmd_oldboot, NULL}, 963 {oldboot_str, cmd_oldboot, NULL},
925 {oneshot_str, cmd_oneshot_reboot, oneshot_help}, 964 {oneshot_str, cmd_oneshot_reboot, oneshot_help},
926 {status_str, cmd_status, NULL}, 965 {status_str, cmd_status, NULL},
927 {hmac_str, cmd_hmac, hmac_help}, 966 {hmac_str, cmd_hmac, hmac_help},